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

时间:2016-07-26 14:35:13

标签: java selenium testng

当我执行 1 4 7 10 and 1 4 7 11 and 1 4 7 12 and 1 4 7 13 and 2 4 8 10 and ... until there are the 108 combinations 时,perms会从其他测试中获取结果。

示例:

.assertAll()

newTest1测试结果:

SoftAssert

newTest2测试结果:

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();
}

有什么想法吗?

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

1 个答案:

答案 0 :(得分:3)

与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();
    // ...
}
相关问题