Thursday, June 4, 2015

TestNG Using Groups to run same test case with different configurations

Real world problem -  One of my colleague wanted to run a test class with different server configurations. Basically he needs to restart the server with different configuration parameters and run the same test class pointing to restarted server. To achieve this, I've suggested TestNG Groups based approach. TestNG listener based approach also considered but had to skip it as it will introduce more listeners to our test structure.

In this example, two TestNG groups were introduced and @BeforeTest & @AfterTest annotations were used to reconfigure the server in each test group execution. Test was design to have single class to include all configuration methods and another test class to hold the tests case. In the test suite XML two test segments were introduced by including groups to run. 



Execution result of above suite as follows 

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
BeforeMethod G1
test1()
test2()
AfterMethod G1
BeforeMethod G2
test1()
test2()
BeforeMethod G2
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.664 sec

Results :

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

No comments: