[英]TestNG Executing Test One Iteration at a time with DataProvider
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.
假设数据提供者中有2行。
@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. 以下是TestNG的xml。
<?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. GroupByListener方法定义如下。
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. 我检查了以下2个问题,它似乎对我不起作用。
TestNG iterate over test data instead of test methods TestNG遍历测试数据而不是测试方法
TestNG - Dataprovider at Class level test annocation TestNG-Class级别的测试提供程序中的Dataprovider
http://fruzenshtein.com/testng-dataprovider-run-tests-sequentially/ http://fruzenshtein.com/testng-dataprovider-run-tests-sequentially/
You should be using a TestNG factory that is powered by a data provider. 您应该使用由数据提供者提供支持的TestNG工厂。
Here's a sample that shows you how to use TestNG factories coupled with a data provider. 这是一个示例,向您展示如何结合数据提供程序使用TestNG工厂。
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文件
<?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. 仅当您在工厂工作时,
group-by-instances=true
属性才有效。 It would cause TestNG to run all the methods within a test class instance together (which is apt in this case). 这将导致TestNG一起运行测试类实例中的所有方法(在这种情况下是合适的)。
Refer to the official TestNG documentation on factories for more information. 有关更多信息,请参考工厂的官方TestNG文档 。
Here's the output 这是输出
...
... TestNG 6.13.1 by Cédric Beust (cedric@beust.com)
...
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
===============================================
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 ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.