SoftAssert正在从其他测试中提取结果



当我执行.assertAll()时,SoftAssert会从其他测试中提取结果。

示例:

public SoftAssert softAssert = new SoftAssert();
@Test(priority = 1)
public void newTest1() {
    softAssert.assertTrue(false, "test1");
    softAssert.assertAll();
}
@Test(priority = 2)
public void newTest2() {
    softAssert.assertTrue(false, "test2");
    softAssert.assertAll();
}

newTest1测试结果:

java.lang.AssertionError: The following asserts failed:
test1 expected [true] but found [false]`

newTest2测试结果:

java.lang.AssertionError: The following asserts failed:
test1 expected [true] but found [false],
test2 expected [true] but found [false]`

有什么想法吗?

testng.version   == 6.9.10
selenium.version == 2.53.1
java.version     == 1.8.0_65-b17

与JUnit不同,TestNG不创建测试类的新实例。因此,如果将SoftAssert直接保存在测试类上,则两个测试方法都将使用相同的SoftAssert实例。使用测试方法实例化SoftAsset inside,或者使用配置方法(如:)对其进行初始化

public SoftAssert softAssert;
@BeforeMethod
public void setup() {
    softAssert = new SoftAssert();
}
@AfterMethod
public void tearDown() {
    softAssert = null;
}
// ...

不过要小心。一般来说,对于TestNG,我发现最好不要保存测试类对象上的任何状态,因为如果您想并行运行方法,就会发生争用。在这种情况下,要么研究TestNG参数注入,要么更详细:

@Test
public void test1() {
    SoftAssert softAssert = new SoftAssert();
    // ...
}
@Test
public void test2() {
    SoftAssert softAssert = new SoftAssert();
    // ...
}

最新更新