gpt4 book ai didi

java - 如何根据内部逻辑多次运行 testNG 测试?

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

我经历了一些关于 的解决方案TestNg 数据提供者 调用次数 , 但是 数据提供者 调用次数 甚至在我的 之前就出现在图片中@测试 方法开始。我的要求是,我有一个 数据阅读器以键值对的形式从 excel 文件中读取数据的类(键总是在第一行,值可以多于一行)。假设如果有 2 行可用的值,那么我将不得不运行相同的 @测试 使用另一组数据(如果我可以为 @Test 的每次迭代运行 @BeforeClass @AfterClass 方法,那就太好了)。

像这样的东西:

@BeforeClass
//Some Code Here that runs on each iteration of @Test

@Test
public void myTest() {

// make a decision here, based on number of rows of values, run the test multiple times
DataReader.LoadDataSheet("TestData.xlsx", "SheetName");

}

@AfterClass
//Some Code Here that runs on each iteration of @Test

最佳答案

您在这里需要的是工厂驱动的数据提供者。

与工厂方法绑定(bind)的第一个数据提供者将提供数据,这些数据将被测试方法用于每个实例以根据需要进行多次迭代。首先由外部数据提供者提供的数据将被作为每个实例一部分的数据提供者使用,该数据提供者将根据需要多次迭代测试。

下面的示例应该能够澄清这一点。

import org.assertj.core.api.Assertions;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class TestClassSample {
private List<String> data;

@Factory(dataProvider = "getDataForInstances")
public TestClassSample(List<String> data) {
this.data = data;
}

@BeforeMethod
public void beforeMethod(Object[] parameters) {
System.err.println("Printing Parameters before running test method " + Arrays.toString(parameters));
}

@Test(dataProvider = "getData")
public void testMethod(String text) {
System.err.println("Printing Parameters when running test method [" + text + "]");
Assertions.assertThat(text).isNotEmpty();
}

@AfterMethod
public void afterMethod(ITestResult result) {
System.err.println("Printing Parameters after running test method " + Arrays.toString(result.getParameters()));
}

@DataProvider(name = "getData")
public Object[][] getData() {
//This data provider simulates the iterations that every test method has to go through based on
//the outer data provider viz., "getDataForInstances()"
Object[][] iterationData = new Object[data.size()][1];
for (int i = 0; i < data.size(); i++) {
iterationData[i] = new String[]{data.get(i)};
}
return iterationData;
}

@DataProvider(name = "getDataForInstances")
public static Object[][] getDataForInstances() {
//This data provider simulates data being read from excel, wherein it would return the number of
//iterations that every test method should go through.
return new Object[][]{
{Collections.singletonList("Java")},
{Arrays.asList("TestNG", "JUnit")},
{Arrays.asList("Maven", "Gradle", "Ant")}
};
}
}

这是输出:
Printing Parameters before running test method [Maven]
Printing Parameters when running test method [Maven]
Printing Parameters after running test method [Maven]

Printing Parameters before running test method [Gradle]
Printing Parameters when running test method [Gradle]
Printing Parameters after running test method [Gradle]

Printing Parameters before running test method [Ant]
Printing Parameters when running test method [Ant]
Printing Parameters after running test method [Ant]

Printing Parameters before running test method [TestNG]
Printing Parameters when running test method [TestNG]
Printing Parameters after running test method [TestNG]

Printing Parameters before running test method [JUnit]
Printing Parameters when running test method [JUnit]
Printing Parameters after running test method [JUnit]

Printing Parameters before running test method [Java]
Printing Parameters when running test method [Java]
Printing Parameters after running test method [Java]

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

关于java - 如何根据内部逻辑多次运行 testNG 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46988155/

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