[英]Junit test class with multiple @Test methods
我有一個Junit測試類,其中包含多個@Test方法,我需要按順序運行。 如果在方法中拋出異常,我想停止整個測試用例並輸出錯誤,但所有其他測試方法都在運行。
public class{
@Test{
//Test1 method`enter code here`
}
@Test{
//Test2 method
}
@Test{
//Test3 method
}
}
如果Test1方法失敗,則不要運行其他測試
注意:所有都是獨立測試
單元測試應設計為彼此獨立運行。 執行順序無法保證。 您應該重新設計測試類,以便順序不重要。
沒有進一步的信息,很難具體告訴你。 但是有一個@before
方法可能有所幫助,該方法在運行每個測試之前檢查一些前提條件。 如果你包含了一個Assume.assumeTrue(...)
方法調用,那么如果條件失敗你的測試可以跳過嗎?
如此處所述 ,JUnit 4.11支持使用Annotation @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;
}
}
如果您需要保留后果並且未通過測試而不使整個測試失敗,請將所有此類測試放在一個測試中並通過假設進行測試。
以下是TESTNG如何指定測試運行順序的示例:
@Test(priority = 1)
public void test1(){}
@Test(priority = 2)
public void test2(){}
@Test(priority = 3)
public void test3(){}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.