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

jjjwad0x  于 2023-05-27  发布在  Java
关注(0)|答案(2)|浏览(137)

例如,我有测试代码

@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”}的测试将失败。我需要测试{“四”},{“五”}将跳过或失败(如果{“三”}失败).我该怎么做?谢谢。

oxalkeyp

oxalkeyp1#

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

你可以这样做。
1.请确保您使用的是TestNG 7.0.0-beta1(这是截至2018年12月16日的最新发布版本)
1.让你的测试类实现org.testng.IHookable接口。
1.现在,在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.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
===============================================
ccrfmcuu

ccrfmcuu2#

有个更简单的办法你可以用ITestResult参数声明一个@AfterMethod方法--这个参数将自动注入测试结果。调用isSuccess()查看方法是否通过。然后,您可以对该信息执行任何您想要的操作,例如通过抛出SkipException来跳过将来的测试。

import org.testng.ITestResult;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static org.junit.jupiter.api.Assertions.fail;

public class TestTest {

    boolean hasFailures = false;

    @AfterMethod
    public void afterMethod(ITestResult result) {
        if (!result.isSuccess()) {
            hasFailures = true;
        }
    }

    @DataProvider
    public Object[][] params() {
        return new Object[][] {
            { 1 }, { 2 }, { 3 }, { 4 }, { 5 },
        };
    }

    @Test(dataProvider = "params")
    public void test(Integer n) {
        if (hasFailures) {
            throw new SkipException("a previous test failed");
        }
        if (n == 2) {
            fail();
        }
    }
}

结果:

PASSED: TestTest.test(1)
FAILED: TestTest.test(2)
<stack trace omitted>

SKIPPED: TestTest.test(3)
org.testng.SkipException: a previous test failed
<stack trace omitted>

SKIPPED: TestTest.test(4)
org.testng.SkipException: a previous test failed
<stack trace omitted>

SKIPPED: TestTest.test(5)
org.testng.SkipException: a previous test failed
<stack trace omitted>

===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 3
===============================================

===============================================
Default suite
Total tests run: 5, Passes: 1, Failures: 1, Skips: 3
===============================================

相关问题