My Requirement:
I have around 100 tests in my test suite.
I don't want to hardcode the group name in the tests source code.
I want to choose the tests that I want to run each time but want to pass that list from an external source (csv/json/xml, etc) to the TestNG program via a maven command.
I can parameterize in the source code if needed, something like below:
@Test(groups = {"param1toBeReadFromExternalFile"})
@BeforeClass(groups = {"param1toBeReadFromExternalFile"})
Is this something that is achievable in TestNG?
Reason for this requirement:
The requirement is for external teams (that don't have access to source code) to be able to tag selective tests from the 100 tests with a specific group name and run tests on the fly without the need to check-out and check-in code.
Here's how you do it.
org.testng.IAnnotationTransformerandorg.testng.IAlterSuiteListenertransform()method, you filter out methods that match the incoming method and then add the groups as found in the data source file from (2).alter()you read a JVM argument that hints at which groups to be filtered and then apply that filter accordingly.Here's a full fledged sample that uses the Google Gson library for json parsing.
Here's my test class
Here's how the listener would look like
Here's how the suite file would look like
Here's how the json mapping would look like