Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using @Parameter annotation or ITestContext to read TestNG configuration file?

I found out that there are 2 ways to read parameters in testng xml configuration file:

  1. use @Parameter annotation and define parameter names after the annotation

  2. use ITestContext as an argument for the test class, DataProvider or Factory and use getCurrentXmlTest() and getParameter() to read the parameter.

Is any of these ways preferred for any reason on the other one? and is there any other way to do it?

like image 244
atavakoli Avatar asked Oct 06 '22 09:10

atavakoli


1 Answers

I think the difference is the scope. If I would need the same parameter across all my suite of tests for eg. env variables, I would use it in a listener implementing ITestListener or ISuiteListener (for suite scope) and then use the ITestContext/Suitecontext to apply the value across all my tests under <test> or under <suite>

However, if I need very specific params for specific testcases, then I would go with @Parameter annotation on those particular testcases.

Also, the @Parameter can only take simple values, so there may be usecases where your tests need different objects (rather than strings) based on some param value. In which case I would prefer a dataprovider, reading the param value( your second option, though argument for testclass i think is incorrect), creating objects and then feeding it to the tests. (for eg. probably name of a properties file is a param value, which is read and objects created based on data in the properties file and fed as different testdata to tests).

like image 117
niharika_neo Avatar answered Oct 11 '22 18:10

niharika_neo