失败后跳过 TestNG 数据提供程序

TestNG dataprovider skip after fail

例如我有测试代码

@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"} 失败)。 我怎样才能做到这一点? 谢谢

注意:当您尝试并行 运行 数据驱动测试时,此解决方案将不起作用。这仅在您 运行 按顺序进行数据驱动测试时才有效。

以下是您可以执行此操作的方法。

  1. 确保您使用的是 TestNG 7.0.0-beta1(这是截至 2018 年 12 月 16 日的最新发布版本)
  2. 让你的测试class实现org.testng.IHookable接口。
  3. 现在,在 run() 方法中设置一个布尔标志,以指示如果您发现异常,则下游方法应该失败(默认情况下,如果测试方法引发异常,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.Whosebug.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.runTestMethod(MethodInvocationHelper.java:237)
    at com.rationaleemotions.Whosebug.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
===============================================