微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在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似乎并不能解决问题,如果整个套件都出现测试失败,它只会停止构建蚂蚁。有什么办法可以在第一次失败时暂停套件执行? 谢谢     

解决方法

您可以对各个测试方法设置依赖性。 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);
    }
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。