TestNG 使用 DataProvider 一次执行一次测试迭代 [英] TestNG Executing Test One Iteration at a time with DataProvider

查看:38
本文介绍了TestNG 使用 DataProvider 一次执行一次测试迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用数据提供程序将数据传递给每个测试方法.假设数据提供程序中有 2 行.

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

下面是TestNG的xml.

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>

GroupByListener 方法定义如下.

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

    }
}

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

I have checked the below 2 questions and it does not seem to work for me.

TestNG 迭代测试数据而不是测试方法

TestNG - 类级别测试通知的数据提供者

http://fruzenshtein.com/testng-dataprovider-run-tests-顺序/

推荐答案

您应该使用由数据提供程序提供支持的 TestNG 工厂.

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.

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 文件

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>

属性 group-by-instances=true 仅在您使用工厂时才有效.这会导致 TestNG 将测试类实例中的所有方法一起运行(在这种情况下很合适).

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

有关工厂的更多信息,请参阅官方 TestNG 文档.

Refer to the official TestNG documentation on factories for more information.

这是输出

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

这篇关于TestNG 使用 DataProvider 一次执行一次测试迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆