正如描述的here,JUnit 4.11支持使用註釋@FixMethodOrder
的有序執行,但其他的都是正確的,所有的測試應該是相互獨立的。
在測試結束時,您可以設置全局成功標誌。該標誌將在每次測試開始時進行測試。如果標誌在一次測試結束時沒有設置(因爲它在結束前失敗),所有其他測試也將失敗。 例子:
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ConsecutiveFail{
private boolean success = true;
@Test
public void test1{
//fist two statements in all tests
assertTrue("other test failed first", success);
success = false;
//do your test
//...
//last statement
success = true;
}
@Test
public void test2{
//fist two statements in all tests
assertTrue("other test failed first", success);
success = false;
//do your test
//...
//last statement
success = true;
}
}