简体   繁体   English

使用不同的数据提供程序在TestNG中重复整个测试类

[英]Repeat entire test class in TestNG, with different data provider

I use TestNG and I would like to run different times the same test and each time use specific data providers which will be used in a subset of test methods, in other words I would like to run the test class with different data without change the test itself. 我使用TestNG,我想在相同的测试中运行不同的时间,并且每次都使用将在测试方法的子集中使用的特定数据提供程序,换句话说,我希望在不更改测试的情况下使用不同的数据运行测试类本身。

I have got a problem creating the factory for the test class, here is my case: 我在为测试类创建工厂时遇到问题,这是我的情况:

@Test
public class MyTest {

    @Factory
    public Object[] createInstances() {
        DataTest dataTest_1 = new DataTest("foo", true);
        DataTest dataTest_2 = new DataTest("FOO", false);

        Object[] result = new Object[]{
                new MyTest(dataTest_1);
                new MyTest(dataTest_2)
        };
        return result;
    }

    private final DataTest dataTest;

    public MyTest(DataTest dataTest) {
        this.dataTest = dataTest;
    }   
}

Error: 错误:

Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNGMapConfigurator@18e8568
org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.InvocationTargetException; nested exception is java.lang.reflect.InvocationTargetException: null
java.lang.reflect.InvocationTargetException
        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:483)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: org.testng.TestNGException:
The factory method class MyTest.createInstances() threw an exception
        at org.testng.internal.FactoryMethod.invoke(FactoryMethod.java:92)
        at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:140)
        at org.testng.TestRunner.initMethods(TestRunner.java:405)
        at org.testng.TestRunner.init(TestRunner.java:231)
        at org.testng.TestRunner.init(TestRunner.java:201)
        at org.testng.TestRunner.<init>(TestRunner.java:150)
        at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:523)
        at org.testng.SuiteRunner.init(SuiteRunner.java:157)
        at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
        at org.testng.TestNG.createSuiteRunner(TestNG.java:1212)
        at org.testng.TestNG.createSuiteRunners(TestNG.java:1199)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1053)
        at org.testng.TestNG.run(TestNG.java:974)
        at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:77)
        at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:110)
        at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:106)
        ... 9 more
Caused by: java.lang.NullPointerException
        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:483)
        at org.testng.internal.FactoryMethod.invoke(FactoryMethod.java:80)
        ... 24 more

Moreover, DataTest component at the moment contains two parameters but it will contains much more parameters - to define the expected values - and a collection of data providers. 此外, DataTest组件目前包含两个参数,但它将包含更多参数-用于定义期望值-以及数据提供者的集合。

Eventually, if I use a String as parameter of the test class - just as a attempt - the test runs. 最终,如果我使用String作为测试类的参数-就像一次尝试-测试运行。

The problem is that you cannot instantiate a new instance of MyTest in order to call createInstances . 问题是您无法实例化MyTest的新实例以调用createInstances So it becomes a chicken and egg problem. 因此,这变成了鸡和鸡蛋的问题。 If you declare createInstances as a static method, it should work. 如果将createInstances声明为静态方法,则它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM