2

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

autoD
  • 109
  • 2
  • 14

2 Answers2

1

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.

juherr
  • 5,640
  • 1
  • 21
  • 63
  • If its not possible to pass data to @BeforeTest than is there any way through which i can pass data to it from data provider. A way through which data can be public and accessed outside dataprovider. – autoD Oct 15 '15 at 09:25
  • I'm not using a `@BeforeTest` in my example, but a `@Test` and simulate it with dependsOnMethods. I think you missmatches `@BeforeTest` and `@BeforeMethod`, check the documentation. – juherr Oct 15 '15 at 09:29
  • i understood but my case is different, here i want to login to the website once ,so that login code i wanted to be in `@beforetest` and carry out my other operations in `@test `which will run more than once ,but since it is not possible to access data from data provider outside,is there any other way through which this can be achieved. regarding this `dependsonmethod` i tried,its giving error – autoD Oct 15 '15 at 09:48
  • You should provide an concrete example – juherr Oct 15 '15 at 11:14
  • Maybe [factories](http://testng.org/doc/documentation-main.html#factories)is what you look for. – juherr Oct 19 '15 at 11:44
0

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());    

  }
Ben Herron
  • 13
  • 5