[英]Soft assertions in base class for multiple tests
我是測試新手,我必須將assertj
框架用於softassertions
。 這些是在多個測試中重復的標准斷言。 對於我定義新的SoftAssertion
的每個測試,執行斷言然后執行.assertAll()
這看起來像是很多樣板代碼。 是否可以在基 class 中抽象斷言函數和assertall()
方法,以便我的測試可以擴展 class?
您可以使用JUnitSoftAssertions
,如下所示: http : JUnitSoftAssertions
我知道這個問題很老了。 但我是來這里尋找答案的,我自己制定了答案。 希望這個答案能幫助別人。 SoftAssertion 可以通過以下方式使用 ThreadLocal 變量抽象到 BaseClass,
public class BaseTest {
ThreadLocal<SoftAssert> softAssert = new ThreadLocal<SoftAssert>();
@BeforeMethod
public void initialise() {
softAssert.set(new SoftAssert());
}
}
public class AppTest extends BaseTest{
@Test(alwaysRun = true)
public void test1() throws Exception {
softAssert.get().fail("test1() I got failed - Test1");
softAssert.get().fail("test1() I got failed - Test1");
softAssert.get().assertAll();
}
@Test(alwaysRun = true)
public void test2() {
softAssert.get().fail("test2 I got failed - Test2");
softAssert.get().fail("test2 I got failed - Test2");
softAssert.get().assertAll();
}
}
**
Output:
Test1 output:
java.lang.AssertionError: The following asserts failed:
test1() I got failed - Test1,
test1() I got failed - Test1
Test2 Output:
java.lang.AssertionError: The following asserts failed:
test2 I got failed - Test2,
test2 I got failed - Test2
**
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.