TestNG Tutorials 37: Usage of Regular Expression With Group Names in TestNG

Hello Folks,

Proper categorisation/grouping of test scripts is very important as it gives us flexibility in running specific or group of tests. Group names followed by meaningful prefix or suffix help us in reducing number of lines of codes to execute desired groups. We will see an example below:

You have multiple groups of tests and you have marked some of tests from each group as smoke tests. A TestNG class is as below:

Now suppose you need to run smoke tests from all groups. We can do this in two ways:

Way 1:

Since there are multiple groups, you need to include each group name in TestNG xml as below:


Creating xml using above approach will be difficult if you have so many groups of tests.

Way 2: Using Regular expression


1. TestNG uses regular expressions, and not wildmats. Be aware of the difference (for example, “anything” is matched by “.*” — dot star — and not “*”).
2. You can’t use regex in the class name attribute with TestNG xml. Regex can be used in include, exclude tags and package tags.
3. Regex is case sensitive means “Smoke” and “smoke” both are different things.

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.