如何在测试失败后停止 TestNG 运行

2024-01-09

我正在尝试在 TestNG 中编写一个测试方法,在失败后 - 整个测试套件将停止运行。

@Test
public void stopTestingIfThisFailed() throws Exception
{
    someTestStesp();
    if (softAsserter.isOneFailed()) {
        asserter.fail("stopTestingIfThisFailed test Failed");
        throw new Exception("Test can't continue, fail here!");
    }
}

正在引发异常,但其他测试方法正在运行。

怎么解决这个问题呢?


您可以使用dependsOnMethods or dependsOnGroups其他测试方法中的注释参数:

@Test(dependsOnMethods = {"stopTestingIfThisFailed"})
public void testAnotherTestMehtod()  {

}

JavaDoc 的dependsOnMethods范围 https://www.javadoc.io/doc/org.testng/testng/7.0.0-beta4:

该方法所依赖的方法列表。无法保证所依赖的方法的运行顺序,但可以保证所有这些方法将在包含此注释的测试方法运行之前运行。此外,如果这些方法中的任何一个不成功,则该测试方法将不会运行并将被标记为“跳过”。如果其中一些方法已被重载,则将运行所有重载的版本。

See https://testng.org/doc/documentation-main.html#dependent-methods https://testng.org/doc/documentation-main.html#dependent-methods

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在测试失败后停止 TestNG 运行 的相关文章

随机推荐