我在測試套件中運行的一些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