gpt4 book ai didi

java - 按顺序对 DataProvider 中的每个数据集运行一组测试

转载 作者:行者123 更新时间:2023-12-02 01:42:27 26 4
gpt4 key购买 nike

我需要在来自 DataProvider 的每个数据之前运行一些测试。
例如,在下面的示例类中,我需要运行 someStart 测试 4 次(在每个数据之前),如何执行此操作?

public class Poligon {

@Test()
public void someStart(){
System.out.println("Start");
}

@DataProvider
public Object[][] getData() {
return new Object[][]{{"a",1},{"c",2},{"d",4},{"x",0}};
}

@Parameters({"p","n"})
@Test(dataProvider = "getData", dependsOnMethods = "someStart")
public void x(String p, int n){
String[] a = {"a","b","c","d"};
int i = 0;
while( i < a.length && !a[i].equals(p)) i++;
i = (i < a.length)? i + 1 : 0;
System.out.println(p + ": " + i);
Assert.assertEquals(i, n);
}

@Test(dependsOnMethods = "x", alwaysRun = true)
public void someEnd(){
System.out.println("End");
}

}

编辑

我需要像这样运行:

Test01 group1
Test02 group1
Test03 group1
DataProvider
Test11 group2 with 1st data set
Test12 group2 with 1st data set
Test13 group2 with 1st data set
Test11 group2 with 2nd data set
Test12 group2 with 2nd data set
Test13 group2 with 2nd data set
...
Test11 group2 with xth data set
Test12 group2 with xth data set
Test13 group2 with xth data set
Test21 group3
Test22 group3
Test23 group3

最佳答案

我在这个网站中找到了答案:www.journaldev.comhowtodoinjava.com 。但这不是我真正需要的。我无法使用不同的 Factory 运行多组测试,但如果没有它,则无法按预期顺序运行。

第一个:使用 Factory 添加类
2en: 在测试类中添加局部变量和构造函数

工厂类:

import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

@Test()
public class Poligon2 {

@Factory(dataProvider = "class-data-provider")
public Object[] getTestClasses(String s, int n) {
Object[] tests = new Object[1];
tests[0] = new Poligon(s, n);
return tests;
}

@DataProvider(name = "class-data-provider")
public Object[][] getData() {
return new Object[][]{{"a", 1}, {"c", 3}, {"d", 4}, {"x", 0}};
}
}

测试类:

import org.testng.Assert;
import org.testng.annotations.*;

public class Poligon {
final String p;
final int n;
int run = 0;
static int instance = 0;
final int current_instance;
static int globalrun = 0;

public Poligon(String p, int n){
this.p = p;
this.n = n;
instance++;
current_instance = instance;
}

@Test()
public void someStart(){
System.out.println("Start");
System.out.println(String.format("Instance: %d; Instance run: %d; Global run %d.", current_instance, run++, globalrun++));
}

@Test(dependsOnMethods = "someStart")
public void x(){
String[] a = {"a","b","c","d"};
int i = 0;
while( i < a.length && !a[i].equals(p)) i++;
i = (i < a.length)? i + 1 : 0;
System.out.println(p + ": " + i);
System.out.println(String.format("Instance: %d; Instance run: %d; Global run %d.", current_instance, run++, globalrun++));
Assert.assertEquals(i, n);
}

@Test(dependsOnMethods = "x", alwaysRun = true)
public void someEnd(){
System.out.println("End");
System.out.println(String.format("Instance: %d; Instance run: %d; Global run %d.", current_instance, run++, globalrun++));
}
}

我得到了预期的结果(使用当前数据集运行测试):

Start
Instance: 2; Instance run: 0; Global run 0.

Start
Instance: 4; Instance run: 0; Global run 1.

Start
Instance: 1; Instance run: 0; Global run 2.

Start
Instance: 3; Instance run: 0; Global run 3.

c: 3
Instance: 2; Instance run: 1; Global run 4.

x: 0
Instance: 4; Instance run: 1; Global run 5.

a: 1
Instance: 1; Instance run: 1; Global run 6.

d: 4
Instance: 3; Instance run: 1; Global run 7.

End
Instance: 2; Instance run: 2; Global run 8.

End
Instance: 4; Instance run: 2; Global run 9.

End
Instance: 1; Instance run: 2; Global run 10.

End
Instance: 3; Instance run: 2; Global run 11.

===============================================
Default Suite
Total tests run: 12, Failures: 0, Skips: 0
===============================================

Process finished with exit code 0

关于java - 按顺序对 DataProvider 中的每个数据集运行一组测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57480380/

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