Is the TestNG data provider parameter possible in the setUp method?

I have some init statements that must be executed with a data provider parameter and would like to access the value of the data provider parameter in the @BeforeMethod installation @BeforeMethod . Is it possible?

+8
testng
source share
2 answers

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

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

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

 @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" ) ); } 
+11
source share

Not. Not.

The right way is to use @ Factory and create an instance of your test class for each dataset, as described here: https://groups.google.com/forum/#!topic/testng-users/3Kny3qTVdmg

-one
source share

All Articles