" dependsOnGroups"与@DataProvider

时间:2017-07-26 22:44:35

标签: java testng

我有3个测试,预计将连续运行。它们位于3个不同的类文件中,所有3个测试都来自另一个类中的dataProviders。我尝试将dependsOnGroupsdataProvider一起使用,如下所示:

 public class DataProviders { 
  @DataProvider(name = "Provider1")
  public static provider[][] A() throws Exception {
    ..............
  }     

  @DataProvider(name = "Provider2")
  public static provider[][] B() throws Exception {    
    .................      
}


@Test(groups="group1") 
public class TestClass1{

  @Test(dataProvider = "Provider1", dataProviderClass = DataProviders.class, enabled = true)
  public void Test1(provider file) throws Exception {
     ...............       
  }


public class TestClass2{

  @Test(groups="group2", dependsOnGroups="group1", dataProvider = "Provider2", dataProviderClass = DataProviders.class, enabled = true)
  public void test2(provider file) throws Exception {       
     ................
  }


public class TestClass3{

  @Test(dependsOnGroups="group2", dataProvider = "Provider2", dataProviderClass = DataProviders.class, enabled = true)
  public void test3(provider file) throws Exception {
     .................
  }

我的testng.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test name="Test">
        <classes>
            <class name="a.b.c.d.TestClass1" />
            <class name="a.b.c.d.TestClass2" />
            <class name="a.b.c.d.TestClass3" />
        </classes>
    </test>
</suite>

testng.xml文件执行测试时,只有第一个测试被执行,我得到以下testNG错误:

org.testng.TestNGException: 
Method main requires 1 parameters but 0 were supplied in the @Test annotation.
    at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:193)
    at org.testng.internal.Parameters.createParameters(Parameters.java:129)
    at org.testng.internal.Parameters.createParameters(Parameters.java:385)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:495)
    at org.testng.internal.Invoker.handleParameters(Invoker.java:1253)
    at org.testng.internal.Invoker.createParameters(Invoker.java:980)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1070)
    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:753)
    at org.testng.TestRunner.run(TestRunner.java:607)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:368)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:363)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:321)
    at org.testng.SuiteRunner.run(SuiteRunner.java:270)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1284)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1209)
    at org.testng.TestNG.runSuites(TestNG.java:1124)
    at org.testng.TestNG.run(TestNG.java:1096)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

如果我将所有测试和dataProviders放在一个java类文件中,它就可以正常工作。

1 个答案:

答案 0 :(得分:1)

看起来你的例子不够饱满。 这个按预期工作。

public class Provider {

    @DataProvider(name = "Provider1")
    public static Object[][] A() throws Exception {
        return new Object[][] { new Object[] {"Provider1-1"}, new Object[] {"Provider1-2"}};
    }

    @DataProvider(name = "Provider2")
    public static Object[][] B() throws Exception {
        return new Object[][] { new Object[] {"Provider2-1"}, new Object[] {"Provider2-2"}};
    }
}

@Test(groups="group1")
public class TestClass1 {
    @Test(dataProvider = "Provider1", dataProviderClass = Provider.class)
    public void Test1(String s) throws Exception {
        System.out.println("Test1 " + s);
    }
}

public class TestClass2 {
    @Test(groups = "group2", dependsOnGroups = "group1", dataProvider = "Provider2", dataProviderClass = Provider.class)
    public void test2(String s) throws Exception {
        System.out.printf("Test2 " + s);
    }
}

public class TestClass3 {
    @Test(dependsOnGroups = "group2", dataProvider = "Provider2", dataProviderClass = Provider.class)
    public void test2(String s) throws Exception {
        System.out.printf("Test3 " + s);
    }
}

使用testng.xml作为你的我有以下输出:

[root]
Test
TestClass1
TestClass1.test1[Provider1-1]
TestClass1.test1[Provider1-2] (1)
TestClass2
TestClass2.test2[Provider2-1]
TestClass2.test2[Provider2-2] (1)
TestClass3
TestClass3.test2[Provider2-1]
TestClass3.test2[Provider2-2] (1)