TestNG Tutorials 29: Grouping Configuration Methods in TestNG

Hello Folks,

In previous post, we learnt about basics of group concepts in TestNG and how to use with @Test annotated methods. In this method we will see how can we use grouping concept with configuration methods.

Below are the configuration methods provided by TestNG:

@BeforeSuite
@AfterSuite
@BeforeTest
@AfterTest
@BeforeGroups
@AfterGroups
@BeforeClass
@AfterClass
@BeforeMethod
@AfterMethod

These configuration methods may be different for each test scripts or/ and common for some test scripts. Suppose, to run “Test Case 1” and “Test Case 2” you need “Test Configuration 1” while for “Test Case 3” and “Test Case 4” you need “Test Configuration 2”.

To achieve this, you have two solutions:

  1. Create a separate base configuration class for test scripts and extend.
  2. Group configuration methods

Create a separate base configuration class for test scripts:

Test Configuration 1 for “Test Case 1” and “Test Case 2”:

Test Case 1:

Testscript1.xml (Testng xml):

Output:

In the same way, you need to create “Test Configuration 2” for “Test Case 3” and “Test Case 4”. You need to create multiple class files if you have so many different configurations.

Grouping configuration methods:

You can group configuration methods ( like we do @Test annotated method) as well and handle group to be included or excluded through testng xml. Let’s understand this through an example:

Configuration methods class:

Test methods:

To run G1 scripts, testng xml is as below:

Output:

To run G2 scripts, testng xml is as below:

Output:

Both approaches are good. Both have their own advantages. Whatever you feel simple to understand and implement you can use.

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Leave a Reply

Your email address will not be published. Required fields are marked *