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

Java TestNG使用DataProvider一次执行一个迭代测试,java,testng,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 thirdT

我使用数据提供者将数据传递给每个测试方法。假设数据提供程序中有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>
我已经检查了以下两个问题,它似乎不适合我


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

@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
}
下面的示例向您展示了如何使用TestNG工厂和数据提供程序

@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
}
package com.rationaliemotions.stackoverflow.qn48399410;
导入org.testng.annotations.DataProvider;
导入org.testng.annotations.Factory;
导入org.testng.annotations.Test;
公共类SampleTestClass{
私有整数迭代;
@工厂(dataProvider=“dp”)
公共SampleTestClass(int迭代){
this.iteration=迭代;
}
@试验
公共测试(){
System.err.println(“为迭代运行的firstTest()”+迭代);
}
@试验
公共测试{
System.err.println(“为迭代#运行的secondTest()”+迭代);
}
@试验
第三次公开考试{
System.err.println(“为迭代#“+迭代运行的第三个测试());
}
@数据提供者(name=“dp”)
公共静态对象[][]getData(){
返回新对象[][]{
{1},
{2},
{3}
};
}
}
这是套件xml文件


属性
groupbyinstances=true
只有在使用工厂时才有效。这将导致TestNG一起运行测试类实例中的所有方法(在本例中这是合适的)

有关更多信息,请参阅有关工厂的说明

这是输出

...
... 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工厂

@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
}
下面的示例向您展示了如何使用TestNG工厂和数据提供程序

@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
}
package com.rationaliemotions.stackoverflow.qn48399410;
导入org.testng.annotations.DataProvider;
导入org.testng.annotations.Factory;
导入org.testng.annotations.Test;
公共类SampleTestClass{
私有整数迭代;
@工厂(dataProvider=“dp”)
公共SampleTestClass(int迭代){
this.iteration=迭代;
}
@试验
公共测试(){
System.err.println(“为迭代运行的firstTest()”+迭代);
}
@试验
公共测试{
System.err.println(“为迭代#运行的secondTest()”+迭代);
}
@试验
第三次公开考试{
System.err.println(“为迭代#“+迭代运行的第三个测试());
}
@数据提供者(name=“dp”)
公共静态对象[][]getData(){
返回新对象[][]{
{1},
{2},
{3}
};
}
}
这是套件xml文件


属性
groupbyinstances=true
只有在使用工厂时才有效。这将导致TestNG一起运行测试类实例中的所有方法(在本例中这是合适的)

有关更多信息,请参阅有关工厂的说明

这是输出

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

谢谢你的回复。我得到了“org.apache.maven.surefire.booter.surefirebooterworkexception:forked进程中有错误”异常。并非所有我的方法都与数据提供程序一起运行。当我删除带有@Factory注释的构造函数方法时,它工作得很好。@Purus-我不太理解你的语句。如果你想要两个或两个以上的方法来迭代每个数据集,那么实现这一点的唯一方法就是使用一个工厂和一个数据提供者。但是,我希望每个实例的结果是分开的。每个案例下有3个案例。感谢您的回复。我得到了“org.apache.maven.surefire.booter.surefirebooterworkexception:forked进程中有错误”异常。并非所有我的方法都与数据提供程序一起运行。当我删除带有@Factory注释的构造函数方法时,它工作得很好。@Purus-我不太理解你的语句。如果你想要两个或两个以上的方法来迭代每个数据集,那么实现这一点的唯一方法就是使用一个工厂和一个数据提供者。但是,我希望每个实例的结果是分开的。每种情况下有3起案件。