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

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

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(&quot;ExampleClients.xlsx&quot;);
    FileInputStream inputStream = new FileInputStream(file);
    XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
    XSSFSheet sheet = workbook.getSheet(&quot;URL&#39;s&quot;);

    int rowCount = sheet.getLastRowNum();
    Object[][] urlData = new Object[rowCount+1][1];

    for (int i = 0; i &lt;= 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=&quot;urlDataProvider&quot;)
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 [][]{{&quot;Input 1&quot;}, {&quot;Input 2&quot;}};
	}
	
	@Test(dataProvider=&quot;returnData&quot;)
	public void iterativeTest(String val) {
		System.out.println(&quot;This is from returned Data : &quot;+ val);
		dp1(val);
		dp2(val);
	}
	
	public void dp1(String val) {
		System.out.println(&quot;Value from dp1 : &quot;+ val);
	}
	
	public void dp2(String val) {
		System.out.println(&quot;Value from dp2 : &quot;+ val);
		Assert.assertEquals(true, true);
	}
}

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:

确定