问题描述
|
我正在使用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);
}
}