Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to use TestNG DataProvider AND test suite Parameters?

Does anyone know if there is a way to use a TestNG DataProvider with a test at the same time as using the @Parameter annotation? Our test suites have some constant configuration information that is being passed to the test methods via the @Parameter annotation. We would now like to use a DataProvider to run these tests over a set of data values.

I understand the internal problem of determining the order the resulting parameters would be in but we need to this feature if possible.

Any thoughts?

In an ideal world, I could do something like this:

@Test(dataprovider = "dataLoader")
@Parameters("suiteParam")
public void testMethod(String suiteParam, String fromDataParam) {
...
}
like image 238
Benjamin Lee Avatar asked Mar 31 '09 18:03

Benjamin Lee


People also ask

Can we use parameters and DataProvider in TestNG?

However, TestNG parameters enable us to pass the values only once per execution cycle. To overcome this, we can use DataProvider in TestNG that allows us to pass multiple parameters to a single test in a single execution.

What is the difference between DataProvider and parameters in TestNG?

What is the difference between DataProvider and Parameter in TestNG? DataProviders pass the different parameters on a single test in a single execution, whereas parameters pass the parameters just once per execution in TestNG.

How do you parameterize DataProvider in TestNG?

Parameterization in TestNG can be achieved in two ways:Using Parameter annotation with TestNG. xml file. Using DataProvider annotation.

Is it possible to run test cases in DataProvider in parallel for each test data given?

Using DataProvider in TestNG, we can easily inject multiple values into the same test case. It comes inbuilt in TestNG and is popularly used in data-driven frameworks. It is an option for the parallel execution of tests in TestNG.


1 Answers

Hey, it may be a bit clunky but why don't you use a @BeforeClass method to store the suiteParam locally on a field of the test class like so.

private String suiteParam;

@BeforeClass
@Parameter("suiteParam")
public void init(String suiteParam) {
  this.suiteParam = suiteParam;
}

This way you can use your data providers in the usual way and still have access to your suite param.

like image 93
mR_fr0g Avatar answered Sep 28 '22 05:09

mR_fr0g