TestNG使用DataProvider一次执行测试一次迭代

时间:2018-01-23 10:26:24

标签: java testng

我正在使用数据提供程序将数据传递给每个测试方法。假设数据提供者中有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
}

目前运行单个测试方法的所有迭代,然后运行第二个测试方法......例如:

firstTest()
firstTest()

secondTest()
secondTest()

thirdTest()
thirdTest()

但我想按以下顺序运行。

firstTest()
secondTest()
thirdTest()

firstTest()
secondTest()
thirdTest()

以下是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>

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) {

    }
}

我检查了以下2个问题,但它似乎对我不起作用。

TestNG iterate over test data instead of test methods

TestNG - Dataprovider at Class level test annocation

http://fruzenshtein.com/testng-dataprovider-run-tests-sequentially/

2 个答案:

答案 0 :(得分:1)

您应该使用由数据提供商提供支持的TestNG工厂。

这是一个示例,向您展示如何使用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}
        };
    }
}

这是套件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>

只有在您使用工厂时,属性group-by-instances=true才会生效。它会导致TestNG一起运行测试类实例中的所有方法(在这种情况下很容易)。

有关详细信息,请参阅工厂的official TestNG documentation

这是输出

...
... 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
===============================================

答案 1 :(得分:1)

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 ?