Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a TestNG data provider parameter in setUp method possible?

Tags:

testng

I have some init statements that need to be done with the data provider parameter and would want to access the data provider parameter value in the @BeforeMethod setup method. Is this possible?

like image 587
stackuser Avatar asked Aug 07 '13 13:08

stackuser


1 Answers

Yes, its totally possible. In the @BeforeMethod annotated method, you can pass an optional built-in argument of Object[] that is basically a copy of the parameters being passed to the @Test method. In my case, I pass 2 args to my test method:

@Test(dataProvider="provider")
public void doTest( TestHelper testHelper, Map<String,String> paramMap ) {
   ....

So, something like this (and it doesn't need to be a factory DataProvider) :

@BeforeMethod
public void setUp( Object[] testArgs ) {
    Map<String,String> paramMap = (Map<String, String>)testArgs[1];
    TestHelper testHelper = testArgs[0];
    String testName = paramMap.get( "testCaseName" );
    log.logTcStep( "Test case name: " + testName );
    log.setLogTcName( testName );
    testHelper.setTestName( testName );
    testHelper.setTagsByString( paramMap.get( "browser" ) );
    testHelper.setBuildNumber( paramMap.get( "environment" ) );
}
like image 104
djangofan Avatar answered Oct 19 '22 15:10

djangofan