英文:
Run every test for a single instance of @DataProvider using a @Factory
问题
我有一个测试类,其中包含大约5个以上的测试,我希望依次对每个URL运行一次。例如:
URL 1 来自@DataProvider:
测试1: 通过
测试2: 失败
测试3: 失败
测试4: 通过
URL 2 来自@DataProvider:
测试1: 通过
测试2: 失败
测试3: 失败
测试4: 通过
而不是目前的方式:
URL 1:
测试1: 通过
URL 2:
测试1: 通过
URL 1:
测试2: 失败
URL 2:
测试2: 失败
等等。
我从Excel表中提取URL,如下所示:
@DataProvider
public Object[][] urlDataProvider() throws Exception {
File file = new File("ExampleClients.xlsx");
FileInputStream inputStream = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheet("URL's");
int rowCount = sheet.getLastRowNum();
Object[][] urlData = new Object[rowCount+1][1];
for (int i = 0; i <= rowCount; i++) {
XSSFRow row = sheet.getRow(i);
XSSFCell cell = row.getCell(0);
String URL = cell.getStringCellValue();
urlData[i][0] = URL;
}
return urlData;
}
我尝试使用@Factory,但对QA/TestNG整个过程很陌生,并感到困惑:
@Factory(dataProvider="urlDataProvider")
public CareFirstTests(String url) {
this.url = url;
}
我的所有测试方法、@Factory和@DataProvider都在同一个Java测试文件中。当我尝试使用Factory时,我收到以下错误消息:
找不到类中的构造函数
我也愿意听取任何其他实现这样的目标的方法。
英文:
I have a test class with about 5+ tests in it that I want to run once for each url sequentially. For example:
> URL 1 From @DataProvider:
Test 1: Pass
Test 2: Fail
Test 3: Fail
Test 4: Pass
URL 2 From @DataProvider:
Test 1: Pass
Test 2: Fail
Test 3: Fail
Test 4: Pass
>
Instead of how it is currently:
> URL 1:
Test 1: Pass
URL 2:
Test 1: Pass
URL 1:
Test 2: Fail
URL 2:
Test 2: Fail
etc.
>
I'm pulling the URLs from an excel sheet in my DataProvider like so:
@DataProvider
public Object[][] urlDataProvider() throws Exception {
File file = new File("ExampleClients.xlsx");
FileInputStream inputStream = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheet("URL's");
int rowCount = sheet.getLastRowNum();
Object[][] urlData = new Object[rowCount+1][1];
for (int i = 0; i <= rowCount; i++) {
XSSFRow row = sheet.getRow(i);
XSSFCell cell = row.getCell(0);
String URL = cell.getStringCellValue();
urlData[i][0] = URL;
}
return urlData;
}
I've tried using a @Factory but am new to the whole QA/TestNG and am confused by it:
@Factory(dataProvider="urlDataProvider")
public CareFirstTests(String url) {
this.url = url;
}
All of my test methods, @Factory and @DataProvider are in the same java test file.
When I try using the Factory I get this error
> Couldn't find a constructor in class
>
I'm also open to hearing any other ways how to accomplish something like this.
答案1
得分: 1
Sure, here is the translated code:
如果没有将您的测试转换为单独的方法的限制,可以使用以下方法来实现上述要求。如果有帮助,请告诉我。
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class StackTest {
@DataProvider
public Object[][] returnData() {
return new Object[][] {{"输入 1"}, {"输入 2"}};
}
@Test(dataProvider = "returnData")
public void iterativeTest(String val) {
System.out.println("这是从返回的数据中获取的: " + val);
dp1(val);
dp2(val);
}
public void dp1(String val) {
System.out.println("来自 dp1 的值: " + val);
}
public void dp2(String val) {
System.out.println("来自 dp2 的值: " + val);
Assert.assertEquals(true, true);
}
}
英文:
If there are no restrictions to convert your tests into individual methods, the above ask can be achieved using below approach. Let me know if this helps.
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class StackTest {
@DataProvider
public Object[][] returnData(){
return new Object [][]{{"Input 1"}, {"Input 2"}};
}
@Test(dataProvider="returnData")
public void iterativeTest(String val) {
System.out.println("This is from returned Data : "+ val);
dp1(val);
dp2(val);
}
public void dp1(String val) {
System.out.println("Value from dp1 : "+ val);
}
public void dp2(String val) {
System.out.println("Value from dp2 : "+ val);
Assert.assertEquals(true, true);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论