我在测试套件中运行的一些JUnit 4测试有问题。JUnit 4测试套件问题
如果我单独运行测试,它们的工作没有问题,但是当它们大部分运行在套件中时,90%的测试方法失败并出错。我注意到,总是第一次测试正常,但其余的都失败了。另一件事是一些测试方法没有以正确的顺序执行(反射不能按预期工作,或者因为方法的检索不一定在创建的顺序中)。如果有多个测试使用同名的方法,通常会发生这种情况。我试图调试一些测试,看起来从一行到下一行,某些属性的值变为null
。
有谁知道这是什么问题,或者如果行为是“正常的”?
在此先感谢。
P.S: OK,测试不依赖于对方,他们没有这样做,他们都有@BeforeClass
,@Before
,@After
,@AfterClass
所以之间的测试一切都清理。测试使用数据库,但在@BeforeClass
的每次测试之前数据库都会被清除,所以这不应该是问题。
Simplefied例如:
测试套件:
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
importy testclasses...;
@RunWith(Suite.class)
@Suite.SuiteClasses({ Test1.class, Test2.class })
public class TestSuiteX {
@BeforeClass
public static void setupSuite() { System.out.println("Tests started"); }
@AfterClass
public static void setupSuite() { System.out.println("Tests started"); }
}
测试: 测试是在Glassfish上运行的服务器应用程序测试functionalily。
现在,这些测试扩展了一个基类,该基类具有清除数据库和登录名的@BeforeClass方法以及只进行注销的@AfterClass。 这不是问题的根源,因为在介绍此课程之前发生了同样的事情。
该类有一些公共静态属性,在其他测试中未使用并实现2个controll方法。
其余的类,在这个例子中,这两个类扩展了基类,并且不支持继承的controll方法。的测试类的
实施例:
imports....
public class Test1 extends AbstractTestClass {
protected static Log log = LogFactory.getLog(Test1.class.getName());
@Test
public void test1_A() throws CustomException1, CustomException2 {
System.out.println("text");
creates some entities with the server api.
deletes a couple of entities with the server api.
//tests if the extities exists in the database
Assert.assertNull(serverapi.isEntity(..));
}
}
和第二:
public class Test1 extends AbstractTestClass {
protected static Log log = LogFactory.getLog(Test1.class.getName());
private static String keyEntity;
private static EntityDO entity;
@Test
public void test1_B() throws CustomException1, CustomException2 {
System.out.println("text");
creates some entities with the server api, adds one entities key to the static attribute and one entity DO to the static attribute for the use in the next method.
deletes a couple of entities with the server api.
//tests if the extities exists in the database
Assert.assertNull(serverapi.isEntity(..));
}
@Test
public void test2_B() throws CustomException1, CustomException2 {
System.out.println("text");
deletes the 2 entities, the one retrieved by the key and the one associated with the static DO attribute
//tests if the deelted entities exists in the database
Assert.assertNull(serverapi.isEntity(..));
}
这是一个基本的例子,实际的测试是较复杂的,但我试图与简化测试和仍然它确实不行。 谢谢。
您可以发布一个测试示例并解释您如何运行测试套件。 – 2010-06-11 10:30:38
该测试套件是一个JUnit 4测试套件,它具有所有测试类的设置,并且有一个@BeforeClass和一个@AfterClass,它只提供一些次要信息(基本字符串)。 – Hypnus 2010-06-11 10:55:41
您发布的代码留下了最重要的细节:1)AbstractTestClass,2)@BeforeClass,@Before,@After,@AfterClass方法,3)keyEntity和实体初始化的方式和时间,4)哪个测试失败,怎么样 ? – 2010-06-11 15:39:11