【发布时间】:2018-09-13 15:12:33
【问题描述】:
我正在使用 TestNG 的软断言,如以下代码。
public class SoftAssertionTest {
SoftAssert softAssert = new SoftAssert();
@Test
public void test1(){
softAssert.assertEquals(2, 3);
softAssert.assertEquals(2, 2);
softAssert.assertEquals(2, 5);
softAssert.assertAll();
}
@Test
public void test2(){
softAssert.assertEquals(2, 2);
softAssert.assertTrue(false);
softAssert.assertAll();
}
}
但是当这个测试类运行时,对于test1,它会返回类似-
java.lang.AssertionError: The following asserts failed:
expected [3] but found [2],
expected [5] but found [2]
这是正确的,但对于test2,它返回test1 和test2 的失败
java.lang.AssertionError: The following asserts failed:
expected [3] but found [2],
expected [5] but found [2],
expected [true] but found [false]
实际上,它应该只返回test2 的结果。
这可以通过在每个测试方法中创建局部变量SoftAssert 来实现。但这可能是代码的重复。
有什么方法可以使用相同的对象,并且在每个测试方法中清除该对象的测试失败结果,以便在新的测试方法中,它只会捕获与该方法相关的失败。
【问题讨论】:
标签: selenium selenium-webdriver automation automated-tests testng