Jail
Jail

Reputation: 900

Using both @DataProvider and @Parameters

I'd like to know whether there is a way to use both @DataProvider and Paramaters to pass parameters.

I've tried two options, but both of these failed:

@Parameters("Brand")
@Test(dataProvider="dpCGA", groups={"CGA"})
public void createAccount(String brand) {
    setBrand(brand);
}

brand variable is being overwritten by the data provider in the above example.

@Parameters("Brand")
@Test(dataProvider="dpCGA", groups={"CGA"})
public void createAccount(String brand, String email) {
    setBrand(brand);
    createAccount(email);
}

Test case didn't even run.

I'm using TestNG to run my test cases, and want to grab the brand parameter from the XML file.

Also, I've got an Excel file which I use to keep emails, and want to grab those values using @DataProvider.

Is it possible to use both of these tags together? If not, is there an other way to grab the brand parameter from the XML file?

Thanks in advance

Upvotes: 4

Views: 8001

Answers (5)

Marwan
Marwan

Reputation: 3

You can create class variables and pass the parameters to the before() method, and inside the before method assign values from the parameters to the class variables, like so:

String classVariableForParam1;

@Parameters({"Param1"})
@BeforeSuite
public void setUp (final String Param1) {
classVariableForParam1= Param1;
}


@Test(dataProvider="data")
public void createAccount(String data) {
    setParam(classVariableForParam1);
    getData(data);
}

Upvotes: 0

Gabriel Furstenheim
Gabriel Furstenheim

Reputation: 3432

Depending on your case it might actually be "doable". In particular, if you only have one instance of "Parameters", for example because you are passing the environment (development, beta...), then you could do:

private String suiteBrand;

@Parameters({"Brand"})
@BeforeSuite
public void setUp (final String Brand) {
   suiteBrand = Brand;
}


@Test(dataProvider="dpCGA", groups={"CGA"})
public void createAccount(String email) {
    brand = suiteBrand;
    setBrand(brand);
    createAccount(email);
}

BeforeSuite will be executed with the parameters and you can access them in the test that is parameterized by the data provider.

It might work with more than one parameter, or with @BeforeEach, but I haven't tried that.

Upvotes: 2

try-catch-finally
try-catch-finally

Reputation: 7634

As other have already pointed out, receiving DataProvider results along with suite parameters is not possible. This answer proves this by stepping through the code:

  1. The Invoker.invokeTestMethods() will call Parameters.handleParameters().
  2. There it will try to lookup a DataProvider
    • If found, it will be invoked. And through it does not add the suite parameters afterwards, it passes the context to the DataProvider
    • If not found, it will resort to the xmlParameters

Though a DataProvider can receive the context via a ITestContext this only makes parameters available configured in the suite (XML), not specified through system properties.

Upvotes: 0

Vignesh Paramasivam
Vignesh Paramasivam

Reputation: 2470

@DataProvider is one of the ways to pass parameters to a method. You cannot use both for same method.

Looking at your question, you can simply add the brand to the DataProvider method something like,

  @DataProvider(name="dpCGA")
  public Object[][] data() {

    return new Object[][] { 
        {"brand", "email1"}, 
        {"brand", "email2"}
     };
  }

and pass it to the method,

@Test(dataProvider="dpCGA", groups={"CGA"})
public void createAccount(String brand, String email) {
    setBrand(brand);
    createAccount(email);
}

Upvotes: 3

QAMate.com
QAMate.com

Reputation: 161

You can not use @DataProvider and @Parameters at the same time. Pass all the parameters through @DataProvider.

Upvotes: 0

Related Questions