關(guān)于 JUnit 測(cè)試中某些測(cè)試方法的順序信賴性有兩種解決方法:
(但是怎么結(jié)果并沒有達(dá)到我要的效果,我希望如果前面的測(cè)試失敗,則后面的所有有信賴于這個(gè)測(cè)試的測(cè)試都失敗)
1、為每個(gè)測(cè)試類添中一個(gè) suite() 方法
public static Test suite() {
TestSuite suite = new TestSuite("edu.dhu.zkl.tools.FileSwithWithMemoryTest");
// 下面添加測(cè)試方法的順序即使 JUnit 運(yùn)行測(cè)試的順序
suite.addTest( new FileSwithWithMemoryTest( "testLoadFileIntoMemory" );
suite.addTest( new FileSwithWithMemoryTest( "testStoreMemoryIntoFile" );
return suite;
}
但是這種方法要將 FileSwithWithMemoryTest 中所有的測(cè)試方法都添加進(jìn)來,當(dāng) FileSwithWithMemoryTest 中
測(cè)試方法多,而要指定有序的測(cè)試方法少時(shí),則讓人感到厭煩。
下面第2種方法是一個(gè)不錯(cuò)的選擇
2、為每個(gè)測(cè)試類添中一個(gè) suite() 方法
public static Test suite() {
String[] orderDependentTests = new String[] {
"testLoadFileIntoMemory",
"testStoreMemoryIntoFile"
};
return new OrderdTestSuite(
FileSwithWithMemoryTest.class, orderDependentTests );
}
這種方法要用到 GSBase jar包(Mike Bowler)