例如,我有要测试的代码
@Test(dataprovider = "getData")
public void test(String data) {
//perform some action using 'data'
}
@DataProvider
public Object[][] getData(){
return new Object[][]{
{"One"},
{"Two"},
{"Three"},
{"Four"},
{"Five"}
};
}
例如,对数据{“ Three”}的测试将失败。 我需要测试{“ Four”},{“ Five”}会跳过或失败(如果{“ Three”}失败)。 我怎样才能做到这一点? 谢谢。
答案 0 :(得分:0)
注意:当您尝试并行运行数据驱动的测试时,此解决方案将不起作用。只有按顺序运行数据驱动的测试,这才起作用。
这是您可以执行的操作。
7.0.0-beta1
(这是截至2018年12月16日的最新版本)org.testng.IHookable
接口。run()
方法中设置了一个布尔标志,以指示如果发现异常,则下游方法应该失败(如果TestNG引发异常,则默认情况下,TestNG会将测试方法标记为失败)下面是一个示例,显示了所有这些操作。
import java.util.Arrays;
import org.testng.Assert;
import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(TestReporter.class)
public class TestclassExample implements IHookable {
private boolean hasFailures = false;
@Test(dataProvider = "getData")
public void test(String data) {
if (data.equals("Three")) {
Assert.fail("Simulating a failure for [" + data + "]");
}
System.err.println("executing test for data [" + data + "]");
}
@DataProvider
public Object[][] getData() {
return new Object[][] {{"One"}, {"Two"}, {"Three"}, {"Four"}, {"Five"}};
}
@Override
public void run(IHookCallBack callBack, ITestResult testResult) {
if (hasFailures) {
testResult.setStatus(ITestResult.FAILURE);
} else {
callBack.runTestMethod(testResult);
if (testResult.getThrowable() != null) {
hasFailures = true;
}
}
}
public static class TestReporter implements ITestListener {
@Override
public void onTestFailure(ITestResult result) {
String msg =
String.format(
"[%s()] failed for data %s",
result.getMethod().getMethodName(), Arrays.toString(result.getParameters()));
System.err.println(msg);
}
}
}
以下是上述代码执行的输出。
executing test for data [One]
executing test for data [Two]
[test()] failed for data [Three]
[test()] failed for data [Four]
[test()] failed for data [Five]
java.lang.AssertionError: Simulating a failure for [Three]
at org.testng.Assert.fail(Assert.java:97)
at com.rationaleemotions.stackoverflow.qn53781839.TestclassExample.test(TestclassExample.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:131)
at org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:237)
at com.rationaleemotions.stackoverflow.qn53781839.TestclassExample.run(TestclassExample.java:36)
at org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:249)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:654)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:792)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1103)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:140)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:122)
at org.testng.TestRunner.privateRun(TestRunner.java:739)
at org.testng.TestRunner.run(TestRunner.java:589)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:398)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:392)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:354)
at org.testng.SuiteRunner.run(SuiteRunner.java:302)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1145)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1067)
at org.testng.TestNG.runSuites(TestNG.java:997)
at org.testng.TestNG.run(TestNG.java:965)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
===============================================
Default Suite
Total tests run: 5, Passes: 2, Failures: 3, Skips: 0
===============================================