gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:06:14 25 4
gpt4 key购买 nike

我有一个测试类和测试方法。这取决于数据提供者方法。数据提供者方法与测试类或其基类之一不同。根据 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

测试类:

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());
}
}

数据提供者类:

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 插件。

最佳答案

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

只需在你的类中添加@Guice:

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

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

关于java - TestNG 非静态数据提供程序方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36565014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com