0

I have several test cases that depend on each other. The order of execution can be defined in TestNG using groups and dependsOnGroups in the @Test annotation:

public class MyTest {
    @Test(groups = { "group1" })
    public void testCase1() {
    }

    @Test(groups = {"group2"}, dependsOnGroups = { "group1" })
    public void testCase2() {
    }

    @Test(groups = {"group2"}, dependsOnGroups = { "group1" })
    public void testCase() {
    }

    @Test(groups = { "group3" }, dependsOnGroups = { "group2" })
    public void testCase4() {
    }
}

But I want it dynamically with only one @Test method and a @DataProvider that gives me the group and the dependencies.

public class MyTest {

    @Test(dataProvider = "test-cases", groups = {testCase[1]}, dependsOnGroups  = {testCase[2]})
    public void executeTest(TestCase testCase) throws Exception {
    }

    @DataProvider(name = "test-cases", parallel = true)
    public Object[][] getTestCases() {
        Object[][] testdata = new Object[...][...];
        // Fields: test name, group, depends on, test data 
        testdata[0] = {"TC#1", "group1", "",       "foo data"};
        testdata[1] = {"TC#2", "group2", "group1", "bar data"};
        testdata[2] = {"TC#3", "group2", "group1", "bzz data"};
        testdata[3] = {"TC#4", "group3", "group2", "frr data"};
        return testdata;
    }

}

Unfortunately this approach does not work, as testCase[1] cannot be accessed from the @Test(..., groups = {testCase[1]}, ...) annotation.

Is there another approach to get data-provider driven tests into a fixed order?

arnep
  • 5,971
  • 3
  • 35
  • 51
  • When you say it doesn't work , do you mean that you are not able to get data from `@Dataprovider` in the above code or you want to not enter data in `@Dataprovider` but want it some other way ? – user1207289 Sep 25 '19 at 14:48
  • @user1207289 I clarified what I want to achieve: get data-driven test cases into a fixed order. – arnep Sep 26 '19 at 08:12

1 Answers1

1

Based on your comments and edit, I think you would like to get data into all your methods and run them in the order they appear in your class.

You can use below approach and testng.xml with preserve-order="true" to get your data driven test to run. Note that you may have to remove groups and dependsOnGroups as below to run this.

public class MyTest {

    @Test(dataProvider = "test-cases")     
    public void testCase1(Object[][] obj) {
        //access obj to get data
    }

    @Test(dataProvider = "test-cases")
    public void testCase2(Object[][] obj1) {
    //access obj1 to get data
    }

    @Test(dataProvider = "test-cases")
    public void testCase(Object[][] obj2) {
     //access obj2 to get data

    }

    @Test(dataProvider = "test-cases")
    public void testCase4(Object[][] obj3) {
         //access obj3 to get data
    }
}

here is your testng.xml that should run it in the order they appear

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="yourSuiteName" >

    <test name="FirstScenario_1" preserve-order="true">

        <classes>  
         <class name="yourPackage.MyTest" >
               <methods> 
                  <include name="testCase1" />
                  <include name="testCase2" /> 
                  <include name="testCase" />
                  <include name="testCase4" />
               </methods> 
        </classes>
    </test>
</suite>
user1207289
  • 3,060
  • 6
  • 30
  • 66
  • Thanks for your suggestion. I do not want to create a test method for every test case as they are dynamically created (e.g. read from spreadsheet or database table) => each data is one test case. Maybe I need another approach for this, but for now the dataProvider is working very well except for I cannot set up the order the test cases are executed in. – arnep Sep 26 '19 at 14:00
  • 1
    @arnep The above xml will set the order up – user1207289 Sep 27 '19 at 15:08