在TestNG中第一次失败后停止套件执行



我使用Ant执行一组TestNG测试,如下所示:

 <testng suitename="functional_test_suite" outputdir="${basedir}/target/"
classpathref="maven.test.classpath" dumpCommand="false" verbose="2"
haltonfailure="true" haltonskipped="false" parallel="methods" threadCount="2">
   <classfileset dir="${basedir}/target/test-classes/">
    <include name="**/*Test.class" />
   </classfileset>

我希望测试在第一次失败后立即停止。Haltonfailure似乎并没有达到目的,它只是在整个套件出现测试失败时停止ant构建。有什么方法可以在第一次失败时停止套件的执行吗?

谢谢

您可以在您的单个测试方法上设置依赖关系。testng依赖性。这将只运行测试方法,如果所需的依赖关系通过。

您可以使用套件侦听器。

public class SuiteListener implements IInvokedMethodListener {
    private boolean hasFailures = false;
    @Override
    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
        synchronized (this) {
            if (hasFailures) {
                throw new SkipException("Skipping this test");
            }
        }
    }
    @Override
    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
        if (method.isTestMethod() && !testResult.isSuccess()) {
            synchronized (this) {
                hasFailures = true;
            }
        }
    }
}
@Listeners(SuiteListener.class)
public class MyTest {
    @Test
    public void test1() {
        Assert.assertEquals(1, 1);
    }
    @Test
    public void test2() {
        Assert.assertEquals(1, 2);  // Fail test
    }
    @Test
    public void test3() {
        // This test will be skipped
        Assert.assertEquals(1, 1);
    }
}

最新更新