Reputation: 5799
I am using data provider to pass data to each of the test methods. Let's assume there are 2 rows in the data provider.
@Test(dataProvider = "TestData")
public void firstTest(String data){
//Code
}
@Test(dataProvider = "TestData")
public void secondTest(String data){
//Code
}
@Test(dataProvider = "TestData")
public void thirdTest(String data){
//Code
}
Currently all iterations of single test method runs and then the second test method runs... For example:
firstTest()
firstTest()
secondTest()
secondTest()
thirdTest()
thirdTest()
But I want to run in the following order.
firstTest()
secondTest()
thirdTest()
firstTest()
secondTest()
thirdTest()
Below is the xml for TestNG.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test-Automation" parallel="methods" thread-count="2" verbose="1">
<test name="Suite Test" parallel="methods" thread-count="2" verbose="1">
<listeners>
<listener class-name="GroupByInstanceEnabler"></listener>
</listeners>
<classes>
<class name="SampleTest">
<methods>
<include name="firstTest"/>
<include name="secondTest"/>
<include name="thirdTest"/>
</methods>
</class>
</classes>
</test>
</suite>
The GroupByListener method is defined as below.
import org.testng.ISuite;
import org.testng.ISuiteListener;
public class GroupByInstanceEnabler implements ISuiteListener {
@Override
public void onStart(ISuite suite) {
System.out.println("Hello");
suite.getXmlSuite().setGroupByInstances(true);
}
@Override
public void onFinish(ISuite suite) {
}
}
I have checked the below 2 questions and it does not seem to work for me.
TestNG iterate over test data instead of test methods
TestNG - Dataprovider at Class level test annocation
http://fruzenshtein.com/testng-dataprovider-run-tests-sequentially/
Upvotes: 0
Views: 2552
Reputation: 21
Krishnan Mahadevan , how can i run the iteration for a data provider using an array inside test class.
for Example
@Test (dataProvider="dp")
public void firstTest() {
System.err.println("firstTest() running for iteration #" + iteration);
array of data provider elements ?
}
is it feasible via dataprovider ?
Upvotes: 1
Reputation: 14736
You should be using a TestNG factory that is powered by a data provider.
Here's a sample that shows you how to use TestNG factories coupled with a data provider.
package com.rationaleemotions.stackoverflow.qn48399410;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
public class SampleTestClass {
private int iteration;
@Factory(dataProvider = "dp")
public SampleTestClass(int iteration) {
this.iteration = iteration;
}
@Test
public void firstTest() {
System.err.println("firstTest() running for iteration #" + iteration);
}
@Test
public void secondTest() {
System.err.println("secondTest() running for iteration #" + iteration);
}
@Test
public void thirdTest() {
System.err.println("thirdTest() running for iteration #" + iteration);
}
@DataProvider(name = "dp")
public static Object[][] getData() {
return new Object[][]{
{1},
{2},
{3}
};
}
}
And here's the suite xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="48399410_Suite" parallel="false" verbose="2">
<test name="48399410_test" verbose="2" group-by-instances="true">
<classes>
<class name="com.rationaleemotions.stackoverflow.qn48399410.SampleTestClass"/>
</classes>
</test>
</suite>
The attribute group-by-instances=true
would have an effect only when you are working with factories. It would cause TestNG to run all the methods within a test class instance together (which is apt in this case).
Refer to the official TestNG documentation on factories for more information.
Here's the output
...
... TestNG 6.13.1 by Cédric Beust ([email protected])
...
firstTest() running for iteration #2
secondTest() running for iteration #2
thirdTest() running for iteration #2
firstTest() running for iteration #3
secondTest() running for iteration #3
thirdTest() running for iteration #3
firstTest() running for iteration #1
secondTest() running for iteration #1
thirdTest() running for iteration #1
PASSED: firstTest
PASSED: secondTest
PASSED: thirdTest
PASSED: firstTest
PASSED: secondTest
PASSED: thirdTest
PASSED: firstTest
PASSED: secondTest
PASSED: thirdTest
===============================================
48399410_test
Tests run: 9, Failures: 0, Skips: 0
===============================================
===============================================
48399410_Suite
Total tests run: 9, Failures: 0, Skips: 0
===============================================
Upvotes: 1