autoD
autoD

Reputation: 119

can dataprovider pass data to @BeforeTest

I want to know if dataprovider can pass data to @BeforeTest along with @Test. I am working on script where in i want pass some data to @BeforeTest and perform some operations. If its possible kindly share the logic. Thank you

Upvotes: 3

Views: 6104

Answers (2)

Ben Herron
Ben Herron

Reputation: 13

It is possible. Pass parameters through your TestNG XML and read them using ITTestContext inside of your @BeforeClass.

Just use

context.getCurrentXmlTest().getParameters()

  @SuppressWarnings("deprecation")
  @BeforeClass
  public void setUp(ITestContext context) {
  System.out.println(context.getCurrentXmlTest().getParameters());    

  }

Upvotes: 0

juherr
juherr

Reputation: 5740

It is not possible to use data providers with @BeforeTest (and maybe any other @BeforeX methods), but you can use a before @Test method, where all other @Test methods are dependent (dependsOnMethods):

@Test(dataProvider="dp")
public void beforeTest(<params...>) {
    <...>
}

@Test(dependsOnMethods="beforeTest") {
    <...>
}

But be careful! TestNG is not JUnit then @BeforeTest != @BeforeMethod.

Upvotes: 1

Related Questions