我有一个测试用例,我添加一个实体,更新它并删除它.因此,执行顺序在这里很重要.我希望它是:
奇怪的是,对于一个测试用例(15个中),JUnit按以下顺序执行:
如何告诉JUnit以特定顺序执行它们?在其他情况下,JUnit完全正常(串行执行).为什么JUnit在这种情况下表现得很奇怪?
以下相关代码段:
private static Date date;
private static int entity;
static Parking p;
public ParkingTests(String name) {
super(name);
}
public void testAdd() throws Exception {
//Add code here
}
public void testUpdate() throws Exception {
//update code here
}
public void testDelete() throws Exception {
//delete code here
}
}
Run Code Online (Sandbox Code Playgroud)
它变得更奇怪了.作为套件的一部分,我运行了很多测试用例.如果我只运行停车箱,则维持订单.如果我和其他人一起运行它,有时会保持,有时不会!
DNA*_*DNA 18
你的情况很尴尬,因为为了隔离测试而保持重复工作感觉很糟糕(见下文) - 但请注意,大多数复制都可以用于setUp和tearDown(@Before,@After)方法,所以你不需要额外的代码.如果测试运行速度太慢而不能经常停止运行,那么最好在清洁测试的名义上浪费一点CPU.
public void testAdd() throws Exception {
// wipe database
// add something
// assert that it was added
}
public void testUpdate() throws Exception {
// wipe database
// add something
// update it
// assert that it was updated
}
public void testDelete() throws Exception {
// wipe database
// add something
// delete it
// assert that it was deleted
}
Run Code Online (Sandbox Code Playgroud)
另一种方法是将所有内容都放在一个带有多个断言的测试中,但这很难理解和维护,并且在测试失败时会提供更少的信息:
public void testCRUD() throws Exception {
// wipe database
// add something
// assert that it was added
// update it
// assert that it was updated
// delete it
// assert that it was deleted
}
Run Code Online (Sandbox Code Playgroud)
使用数据库或集合或任何类型的存储进行测试是棘手的,因为一个测试总是会通过将垃圾留在数据库/集合中来影响其他测试.即使您的测试没有明确地相互依赖,它们仍可能相互干扰,尤其是当其中一个失败时.
在可能的情况下,为每个测试使用一个新实例,或者擦除数据,理想情况下尽可能简单 - 例如对于数据库,擦除整个表比删除一个非常特定的删除更容易成功,您可能会意外地出错.
更新:通常在测试开始时擦除数据会更好,因此一次测试运行失败不会影响下一次运行.
eis*_*eis 12
如果你确定你想要测试的执行顺序,JUnit 4.11现在通过注释支持这个.有关更多讨论,请参阅此主题 - 基本上,您将使用
@FixMethodOrder
Run Code Online (Sandbox Code Playgroud)
以这种方式保证一些测试订单.但是气馁.
小智 5
通常,JUnit不保证测试用例的排序.它不保证按字母顺序排列,也不保证文件中的顺序.如果测试的顺序很重要,那么一个依赖于前一个的输出.如果第一个失败怎么办?我们是否应该为后来的(和依赖的)测试烦恼?可能不是.
所以,如果我们有这个:
@Test
public void first(){...}
@Test
public void second() {...}
@Test
public void third() {...}
Run Code Online (Sandbox Code Playgroud)
我们不知道它们将以什么顺序运行.由于我们希望它们按顺序排列,如果前一个失败,我们应该不会打扰第二个或第三个,我们可以这样做:
@Test
public void firstThree(){
first();
second();
third();
}
public void first(){...}
public void second() {...}
public void third() {...}
Run Code Online (Sandbox Code Playgroud)
请注意,这次我们只有一个@Test,它保证了订购.
| 归档时间: |
|
| 查看次数: |
57945 次 |
| 最近记录: |