gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:21:53 25 4
gpt4 key购买 nike

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

最佳答案

您应该使用由数据提供者提供支持的 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
===============================================

关于java - TestNG 使用 DataProvider 一次执行一个测试迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48399410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com