TestNG 非静态数据提供程序方法不起作用

TestNG non static data provider method not working

我有一个测试class和测试方法。这取决于数据提供者方法。数据提供者方法与测试 class 或其基础 class 之一不同 class。根据 TestNG 文档

If you want to put your data provider in a different class, it needs to be a static method or a class with a non-arg constructor, and you specify the class where it can be found in the dataProviderClass attribute

测试class:

package test;

import org.testng.Assert;
import org.testng.annotations.Test;

import dataprovider.TestDataProvider;

public class TestClass {
      @Test(dataProvider = "get", dataProviderClass=TestDataProvider.class)
      public void test1(String s1, String s2) {
          Assert.assertEquals(s1.length(), s2.length());
      }
}

数据提供者class:

package dataprovider;

import org.testng.annotations.DataProvider;

public class TestDataProvider {
    @DataProvider(name = "get")
    public Object[][] testData() {
        return new Object[][] {{"a", "b"},{"c", "d"}};
    }
}

来自 Eclipse 的测试输出:

    java.lang.RuntimeException: Can't invoke method public java.lang.Object[][] dataprovider.
TestDataProvider.testData() on this instance of class test.TestClass due to class mismatch
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:75)
    at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:137)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:430)
    at org.testng.internal.Invoker.handleParameters(Invoker.java:1243)
    at org.testng.internal.Invoker.createParameters(Invoker.java:992)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1082)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:778)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1225)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1150)
    at org.testng.TestNG.runSuites(TestNG.java:1075)
    at org.testng.TestNG.run(TestNG.java:1047)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)

好像不行。我正在使用 TestNG 6.9.11 并尝试从 Maven 和 Eclipse 运行。我正在使用最新的 Eclipse-TestNG 插件。

您已经回答了您的问题,

If you want to put your data provider in a different class, it needs to be a static method or a class with a non-arg constructor.

并且根据 Java 设计它不应该工作,导致从其他 class 调用方法要么该方法应该是静态的,要么我们需要那个 class 的对象。

testData 方法不是静态的,因此不能使用 class 调用它,并且您没有创建非参数构造函数,因此无法创建对象(没有对象非静态方法无法调用)。

希望您理解我的观点并得到您的答案:)

免责声明:我是 added the support of non static methods from external dataProvider

只需在您的 class 上添加 @Guice:

@Guice
public class TestClass {
    @Test(dataProvider = "get", dataProviderClass=TestDataProvider.class)
    public void test1(String s1, String s2) {
        Assert.assertEquals(s1.length(), s2.length());
    }
}

这是一个尚未被引用的问题。

  1. TestNG 首先在同一个 class 中寻找数据提供者。 例如:
public class DummyTest {

  @DataProvider(name = "data")
  public Object[][] dummyData() {
      return new Object[][]{
              {"Khan"}, {"Kapoor"}
      };
  }

  @Test(dataProvider = "data")
  public void dataProviderTest(String name){
      System.out.println("My Name is " + name);
  }
  1. 如果在同一个 class 中找不到数据提供者,那么 TestNG 会在基础 class 中查找。例如:
public class DummyData {

    @DataProvider(name = "data")
    public Object[][] dummyData() {
        return new Object[][]{
                {"Khan"}, {"Kapoor"}
        };
    }
}
public class DummyTest extends DummyData {
    @Test(dataProvider = "data")
    public void dataProviderTest(String name){
        System.out.println("My Name is " + name);
    }
}
  1. 如果您的 DataProvider 具有默认或无参数构造函数,您可以将数据提供程序方法声明为静态或非静态。例如
public class DummyData {

    @DataProvider(name = "data")
    public Object[][] dummyData() {
        return new Object[][]{
                {"Khan"}, {"Kapoor"}
        };
    }
}
public class DummyTest {
    @Test(dataProvider = "data", dataProviderClass = DummyData.class)
    public void dataProviderTest(String name){
        System.out.println("My Name is " + name);
    }
}
  1. 如果 DataProvider class 具有参数化构造函数,请将 dataprovider 方法声明为静态,否则 jvm 将抛出错误。来自 intellij 的错误快照:
objc[48034]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java (0x1037f04c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x1038b84e0). One of the two will be used. Which one is undefined.
[Utils] [ERROR] [Error] java.lang.InstantiationException: apitestset.users.DummyData
    at java.lang.Class.newInstance(Class.java:427)
    at org.testng.internal.ClassHelper.newInstance(ClassHelper.java:69)
    at org.testng.internal.Parameters.findDataProvider(Parameters.java:582)
    at org.testng.internal.Parameters.findDataProvider(Parameters.java:509)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:691)
    at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:49)
    at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:37)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:924)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
Caused by: java.lang.NoSuchMethodException: apitestset.users.DummyData.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    ... 23 more


org.testng.TestNGException: 
Cannot instantiate class apitestset.users.DummyData

    at org.testng.internal.ClassHelper.newInstance(ClassHelper.java:71)
    at org.testng.internal.Parameters.findDataProvider(Parameters.java:582)
    at org.testng.internal.Parameters.findDataProvider(Parameters.java:509)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:691)
    at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:49)
    at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:37)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:924)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
Caused by: java.lang.InstantiationException: apitestset.users.DummyData
    at java.lang.Class.newInstance(Class.java:427)
    at org.testng.internal.ClassHelper.newInstance(ClassHelper.java:69)
    ... 22 more
Caused by: java.lang.NoSuchMethodException: apitestset.users.DummyData.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    ... 23 more

对于收到相同错误但无法按照其他答案修复的其他人:

我注意到在 Eclipse 中将我的 TestNG 插件从 6.X 升级到 7.0 后,这个错误开始出现。我尝试了其他解决方案,但唯一对我有用的是完全卸载 TestNG(插件、TestNg Maven 等),然后将更新站点设置为该特定版本的 TestNG。