TestNG iterates over test data instead of test methods

When using a DataProvider with multiple TestNG methods, each method starts with all the data sets in the sequence. Instead, I want to iterate over datasets and execute all the methods on each iteration. I do not care if the results show each test result or a summary of the runs by the method.

I have already tried the option

order-by-instances="true"

in suite.xml without success.

Code example:

public class TestNGTest
{
    @DataProvider(name = "dp")
    public Object[][] createData(Method m) {
      return new Object[][] { new Object[] { "Cedric" }, new Object[] {"Martina"}};
    }

    @Test(dataProvider = "dp")    
    public void test1(String s) throws InterruptedException {
        System.out.println("test1 " + s);
        Thread.sleep(1000);
    }

    @Test(dataProvider = "dp")
    public void test2(String s) throws InterruptedException {
        System.out.println("test2 " + s);
        Thread.sleep(1000);
    }
}

Actual result:

test1 Cedric
test1 Martina
test2 Cedric
test2 Martina
PASSED: test1("Cedric")
PASSED: test1("Martina")
PASSED: test2("Cedric")
PASSED: test2("Martina")

Required Result:

test1 Cedric
test2 Cedric
test1 Martina
test2 Martina
PASSED: test1("Cedric")
PASSED: test2("Cedric")
PASSED: test1("Martina")
PASSED: test2("Martina")
+4
source share
2 answers

GroupByInstanceEnabler. Listeners ( , ) META-INF, TestNg ServiceLoader (http://testng.org/doc/documentation-main.html#listeners-service-loader)

suite.xml , , META-INF enabler . , , - - IDE, - .

import org.testng.ISuite;
import org.testng.ISuiteListener;

public class GroupByInstanceEnabler implements ISuiteListener {

    @Override
    public void onStart(ISuite suite) {
        suite.getXmlSuite().setGroupByInstances(true);
    }

    @Override
    public void onFinish(ISuite suite) {

    }
}

Pawel

+1

, Factory, DataProvider XmlTest.

, TestClass . , factory . , .

package com.nhp.ts.test.business.qi;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.testng.xml.XmlTest;

public class FooTest {

    @DataProvider(name = "dp")
    public Object[][] createData() {
        return new Object[][] {
            { "Cedric" },
            { "Martina" }
        };
    }

    @Factory(dataProvider = "dp")
    public TestClass[] testFactory(String name) {
        return new TestClass[] { new TestClass(name) };
    }

    public class TestClass {

        private String name;

        public TestClass(String name) {
            this.name = name;
        }

        @BeforeTest
        public void setOptions(XmlTest test)
        {
            test.setGroupByInstances(true);
        }

        @Test
        public void test1() throws InterruptedException {
            System.out.println("test1 " + name);
        }

        @Test
        public void test2() throws InterruptedException {
            System.out.println("test2 " + name);
        }
    }
}
0

All Articles