I*******o 发帖数: 53 | 1 情况应该是比较common的,就是假设你要测试class A B C D E F
测试A需要有B和C的objects在db,
测试B需要有D和C的objects在db,
测试C需要有D, F和E的objects在db, etc...
那么这些test case怎么prepare test data呢?
现在的做法是在TestA的setup里面来
b1=insert(b); b2=insert(b); c1=insert(c); c2=insert(c);
a1=new A(b1, c1); a2=newA(b2, c2);
大概这样吧。可是这样一来有很多的重复代码,看着也不太clean,
尤其当这种data dependency复杂一些的话。
做了点research,看到一个叫Dependent Object framework的open source,
大概是定义a1.xml, a2.xml, b1.xml, b2.xml之类的xml,
这些xml定义这些test objects的data & dependency,有谁有经验么? | m******t 发帖数: 2416 | 2 In most of the cases you would want to take the database out of
the picture completely, by mocking the data access layer.
If the cases you are trying to run are all about the database,
e.g., the data access layer itself, I would populate the test db
with some sql scripts before kicking off the tests, instead of
relying on one test case to set up data for another.
【在 I*******o 的大作中提到】 : 情况应该是比较common的,就是假设你要测试class A B C D E F : 测试A需要有B和C的objects在db, : 测试B需要有D和C的objects在db, : 测试C需要有D, F和E的objects在db, etc... : 那么这些test case怎么prepare test data呢? : 现在的做法是在TestA的setup里面来 : b1=insert(b); b2=insert(b); c1=insert(c); c2=insert(c); : a1=new A(b1, c1); a2=newA(b2, c2); : 大概这样吧。可是这样一来有很多的重复代码,看着也不太clean, : 尤其当这种data dependency复杂一些的话。
| g**********y 发帖数: 14569 | 3 楼上说得对,你是在把两个test(database test + business logic test)合并成一个
test, 这跟unit test是背道而驰的。
如果我做,我会写个mock object或接口,模拟数据,测试business logic. 另外专门
写code, 测试database连接,读写。 | b******y 发帖数: 1684 | 4 if there is data access tier (or persistence tier),
how do you test your DAO stuff?
【在 g**********y 的大作中提到】 : 楼上说得对,你是在把两个test(database test + business logic test)合并成一个 : test, 这跟unit test是背道而驰的。 : 如果我做,我会写个mock object或接口,模拟数据,测试business logic. 另外专门 : 写code, 测试database连接,读写。
| S********a 发帖数: 1163 | 5 using mock objects. u want to test A only in A's unit test. so mock up B C D
E F.
【在 I*******o 的大作中提到】 : 情况应该是比较common的,就是假设你要测试class A B C D E F : 测试A需要有B和C的objects在db, : 测试B需要有D和C的objects在db, : 测试C需要有D, F和E的objects在db, etc... : 那么这些test case怎么prepare test data呢? : 现在的做法是在TestA的setup里面来 : b1=insert(b); b2=insert(b); c1=insert(c); c2=insert(c); : a1=new A(b1, c1); a2=newA(b2, c2); : 大概这样吧。可是这样一来有很多的重复代码,看着也不太clean, : 尤其当这种data dependency复杂一些的话。
| S********a 发帖数: 1163 | 6 that's how ur framework should be. always provide a DAO, a facade.
【在 b******y 的大作中提到】 : if there is data access tier (or persistence tier), : how do you test your DAO stuff?
| b******y 发帖数: 1684 | 7 I can mock up B C D E F objects, but if they are not in DB,
I won't be able to insert a (object of A) into DB due to
referencial integrity, right?
D
【在 S********a 的大作中提到】 : using mock objects. u want to test A only in A's unit test. so mock up B C D : E F.
| g*****g 发帖数: 34805 | 8 If you are testing DAO layer, you should insert all objects
you are supposed to insert and query to verify.
【在 b******y 的大作中提到】 : I can mock up B C D E F objects, but if they are not in DB, : I won't be able to insert a (object of A) into DB due to : referencial integrity, right? : : D
| b******y 发帖数: 1684 | 9 so my Q is, since you need to insert lots of objects.
how to reuse the inserts?
【在 g*****g 的大作中提到】 : If you are testing DAO layer, you should insert all objects : you are supposed to insert and query to verify.
| S********a 发帖数: 1163 | 10 u can have an abstract class as the parent class, who just has setUp()
method, then each unit test extends from it. so u reuse the setUp() leh.
【在 b******y 的大作中提到】 : so my Q is, since you need to insert lots of objects. : how to reuse the inserts?
| | | b******y 发帖数: 1684 | 11 ok, we are getting into details...
假设你要测试class A B C D E F
测试A需要有B和C的objects在db,
测试B需要有D和C的objects在db,
测试C需要有D, F和E的objects在db, etc...
what does this BaseTest.setUp do?
【在 S********a 的大作中提到】 : u can have an abstract class as the parent class, who just has setUp() : method, then each unit test extends from it. so u reuse the setUp() leh.
| S********a 发帖数: 1163 | 12 public void setUp() {
insertData(A);
insertData(B);
insertData(C);
insertData(D);
insertData(E);
insertData(F);
}
me ft. u ask so basic a Q?
【在 b******y 的大作中提到】 : ok, we are getting into details... : 假设你要测试class A B C D E F : 测试A需要有B和C的objects在db, : 测试B需要有D和C的objects在db, : 测试C需要有D, F和E的objects在db, etc... : what does this BaseTest.setUp do?
| b******y 发帖数: 1684 | 13 ft, I need to test insertData(A) for ADao.
I also need to test insertData(B) for BDao.
and C, D, E, ...
now you put them all in setUp le.
【在 S********a 的大作中提到】 : public void setUp() { : insertData(A); : insertData(B); : insertData(C); : insertData(D); : insertData(E); : insertData(F); : } : me ft. u ask so basic a Q?
| g*****g 发帖数: 34805 | 14 Put all these intertData functions as static in some utility
class then.
【在 b******y 的大作中提到】 : ft, I need to test insertData(A) for ADao. : I also need to test insertData(B) for BDao. : and C, D, E, ... : now you put them all in setUp le.
| k***r 发帖数: 4260 | 15 He meant how to avoid having to do the setup and teardown
again and again and again, when you know that they can be
re-used. But the tests appear to be independent and they
don't know each other so each of them does set up and
teardown. For this problem, I don't have a good answer.
【在 g*****g 的大作中提到】 : Put all these intertData functions as static in some utility : class then.
| b******y 发帖数: 1684 | 16 nod nod
【在 k***r 的大作中提到】 : He meant how to avoid having to do the setup and teardown : again and again and again, when you know that they can be : re-used. But the tests appear to be independent and they : don't know each other so each of them does set up and : teardown. For this problem, I don't have a good answer.
| m******t 发帖数: 2416 | 17
I would try not to confuse test fixtures and
test artifacts.
The object B1 you insert in TestA.setUp is
a test fixture for TestA, i.e. something to
support a test.
The object B2 inserted by TestB.testInsertB() is
a test artifact, i.e., the result of a test.
It is actually a good thing to keep those two
separated.
【在 b******y 的大作中提到】 : ft, I need to test insertData(A) for ADao. : I also need to test insertData(B) for BDao. : and C, D, E, ... : now you put them all in setUp le.
| l***i 发帖数: 289 | 18 如果数据量不大的话可以直接把那些对象全部放入数据库,每个测试都可以用,而不用
针对特定的测试初始化数据。
如果性能影响比较大的话再考虑优化吧。
【在 I*******o 的大作中提到】 : 情况应该是比较common的,就是假设你要测试class A B C D E F : 测试A需要有B和C的objects在db, : 测试B需要有D和C的objects在db, : 测试C需要有D, F和E的objects在db, etc... : 那么这些test case怎么prepare test data呢? : 现在的做法是在TestA的setup里面来 : b1=insert(b); b2=insert(b); c1=insert(c); c2=insert(c); : a1=new A(b1, c1); a2=newA(b2, c2); : 大概这样吧。可是这样一来有很多的重复代码,看着也不太clean, : 尤其当这种data dependency复杂一些的话。
| l***i 发帖数: 289 | 19 另外从这段代码:
a2=newA(b2, c2)
上看,a2只是需要b2,c2两个实例,至于实例是从数据库读出来的,还是直接new初来得
,应该区别不大。
要简化Fixture Setup,Object Mother可以帮忙:
http://martinfowler.com/bliki/ObjectMother.html
【在 I*******o 的大作中提到】 : 情况应该是比较common的,就是假设你要测试class A B C D E F : 测试A需要有B和C的objects在db, : 测试B需要有D和C的objects在db, : 测试C需要有D, F和E的objects在db, etc... : 那么这些test case怎么prepare test data呢? : 现在的做法是在TestA的setup里面来 : b1=insert(b); b2=insert(b); c1=insert(c); c2=insert(c); : a1=new A(b1, c1); a2=newA(b2, c2); : 大概这样吧。可是这样一来有很多的重复代码,看着也不太clean, : 尤其当这种data dependency复杂一些的话。
| l***i 发帖数: 289 | 20 取决于这些object的类型。因为需要保存到数据库,那么这些对象是domain object的
可能性比较打,如果是domain object,那么就不要mock了。mock只有在想确认“交互
”正确地时候用处才比较大。
D
【在 S********a 的大作中提到】 : using mock objects. u want to test A only in A's unit test. so mock up B C D : E F.
| l***i 发帖数: 289 | 21 单元测试这么做没问题,不过还是要在集成测试里将这些类连在一起测的。
【在 g**********y 的大作中提到】 : 楼上说得对,你是在把两个test(database test + business logic test)合并成一个 : test, 这跟unit test是背道而驰的。 : 如果我做,我会写个mock object或接口,模拟数据,测试business logic. 另外专门 : 写code, 测试database连接,读写。
|
|