使用 @Factory 运行针对 @DataProvider 的单个实例的每个测试。

huangapple go评论92阅读模式
英文:

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,如下所示:

  1. @DataProvider
  2. public Object[][] urlDataProvider() throws Exception {
  3. File file = new File("ExampleClients.xlsx");
  4. FileInputStream inputStream = new FileInputStream(file);
  5. XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
  6. XSSFSheet sheet = workbook.getSheet("URL's");
  7. int rowCount = sheet.getLastRowNum();
  8. Object[][] urlData = new Object[rowCount+1][1];
  9. for (int i = 0; i <= rowCount; i++) {
  10. XSSFRow row = sheet.getRow(i);
  11. XSSFCell cell = row.getCell(0);
  12. String URL = cell.getStringCellValue();
  13. urlData[i][0] = URL;
  14. }
  15. return urlData;
  16. }

我尝试使用@Factory,但对QA/TestNG整个过程很陌生,并感到困惑:

  1. @Factory(dataProvider="urlDataProvider")
  2. public CareFirstTests(String url) {
  3. this.url = url;
  4. }

我的所有测试方法、@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:

  1. @DataProvider
  2. public Object[][] urlDataProvider() throws Exception {
  3. File file = new File(&quot;ExampleClients.xlsx&quot;);
  4. FileInputStream inputStream = new FileInputStream(file);
  5. XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
  6. XSSFSheet sheet = workbook.getSheet(&quot;URL&#39;s&quot;);
  7. int rowCount = sheet.getLastRowNum();
  8. Object[][] urlData = new Object[rowCount+1][1];
  9. for (int i = 0; i &lt;= rowCount; i++) {
  10. XSSFRow row = sheet.getRow(i);
  11. XSSFCell cell = row.getCell(0);
  12. String URL = cell.getStringCellValue();
  13. urlData[i][0] = URL;
  14. }
  15. return urlData;
  16. }

I've tried using a @Factory but am new to the whole QA/TestNG and am confused by it:

  1. @Factory(dataProvider=&quot;urlDataProvider&quot;)
  2. public CareFirstTests(String url) {
  3. this.url = url;
  4. }

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:

  1. 如果没有将您的测试转换为单独的方法的限制可以使用以下方法来实现上述要求如果有帮助请告诉我
  2. import org.testng.Assert;
  3. import org.testng.annotations.DataProvider;
  4. import org.testng.annotations.Test;
  5. public class StackTest {
  6. @DataProvider
  7. public Object[][] returnData() {
  8. return new Object[][] {{"输入 1"}, {"输入 2"}};
  9. }
  10. @Test(dataProvider = "returnData")
  11. public void iterativeTest(String val) {
  12. System.out.println("这是从返回的数据中获取的: " + val);
  13. dp1(val);
  14. dp2(val);
  15. }
  16. public void dp1(String val) {
  17. System.out.println("来自 dp1 的值: " + val);
  18. }
  19. public void dp2(String val) {
  20. System.out.println("来自 dp2 的值: " + val);
  21. Assert.assertEquals(true, true);
  22. }
  23. }
英文:

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.

  1. import org.testng.Assert;
  2. import org.testng.annotations.DataProvider;
  3. import org.testng.annotations.Test;
  4. public class StackTest {
  5. @DataProvider
  6. public Object[][] returnData(){
  7. return new Object [][]{{&quot;Input 1&quot;}, {&quot;Input 2&quot;}};
  8. }
  9. @Test(dataProvider=&quot;returnData&quot;)
  10. public void iterativeTest(String val) {
  11. System.out.println(&quot;This is from returned Data : &quot;+ val);
  12. dp1(val);
  13. dp2(val);
  14. }
  15. public void dp1(String val) {
  16. System.out.println(&quot;Value from dp1 : &quot;+ val);
  17. }
  18. public void dp2(String val) {
  19. System.out.println(&quot;Value from dp2 : &quot;+ val);
  20. Assert.assertEquals(true, true);
  21. }
  22. }

huangapple
  • 本文由 发表于 2023年2月10日 05:20:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定