开发者

Stop Suite Execution After First Failure In TestNG

I am using Ant to execute a set of TestNG tests as follows:

 <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>

I would like for the tests to stop immediately after the first failure. haltonfailure does not seem to do the trick, it just 开发者_Go百科halts the ant build if the whole suite has test failures. Is there any way I can halt the suite execution on first failure?

Thanks


You can set dependencies on your individual test methods. testng dependencies. This will only run test methods if desired dependencies passed.


You can use a suite listener for this purpose.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜