TestNG:如果一个测试抛出异常,请继续其他测试

问题描述 投票:1回答:1

在TestNG中,如果一个测试失败,我想继续进行其他测试,以便可以检查所有测试都失败了。

我该怎么办?如果可以通过某些TestNG配置来完成,那会更好。

testng
1个回答
0
投票

如果您的测试由于异常(或其他原因)而失败,除非您有依赖关系,否则它将毫无问题地跳入下一个测试。

如果我错了,请纠正我,但我想您想问:“如果我的测试中的验证失败,我希望它继续进行其余的测试。”

如果是这种情况,您要使用的是Soft Assertions。该对象将收集您的所有错误,并且只会失败并在告诉您时将其显示。

import org.testng.asserts.SoftAssert;

public class ShoppingCartValidation {

@Test
public void testA() {
    SoftAssert softAssert = new SoftAssert();

    int a = 5;
    int b = 0;
    int c = 0;

    softAssert.assertTrue(b>a,"b should be greater than a.");

    try {
        c = a/b;
    } catch (ArithmeticException e) {
        softAssert.fail("There was an exception trying to divide " + a + " by " + b);
    }

    softAssert.assertEquals(c, 10, "b should be equals 10.");

    softAssert.assertAll(); // This line will make the test fail if any of the validations above failed, and will show all the failures.

}

}

错误输出:TestNG Soft Assert validation error output

希望能有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.