Executing the Testcases at Package level with regex - rohitkrbhardwaj09/TestNG_ByRSA GitHub Wiki
In large test suites (e.g., 100+ test cases), you may be asked to:
-
Exclude a specific module's test cases (e.g., Mobile, API)
-
Manually excluding each test is tedious and error-prone.
-
Establish consistent naming for test methods.
-
For example, all mobile-related test methods should start with:
mobileLogin() mobileSignIn() mobilePay()
-
Similarly for API, use:
apiLogin(), apiGetData(), etc.
<methods>
<exclude name="mobile.*" />
</methods>
-
mobile.*
will match all test methods starting withmobile
-
.*
means any character repeated 0 or more times -
Result: All methods starting with "mobile" will be excluded from execution
-
Suppose
Day3.java
contains 100 test methods -
30 of them belong to the Mobile module
-
Instead of excluding 30 methods individually, use:
<exclude name="mobile.*" />
-
To exclude API tests:
<exclude name="api.*" />
-
Initially, 7 test cases run.
-
Added 3 mobile tests:
mobileSignIn
,mobileSignOut
, etc. -
After applying
<exclude name="mobile.*" />
:-
Total test count still 7
-
The 3 mobile test cases are ignored
-
-
Changed to
<exclude name="api.*" />
:-
API test cases now excluded
-
Mobile test cases included again
-
-
When running regression tests, you want to execute all tests in a package, regardless of individual class or method.
<suite name="Test Suite">
<test name="Regression Suite">
<packages>
<package name="test" />
</packages>
</test>
</suite>
-
Executes all test methods from all classes inside
test
package. -
Helps avoid listing each test class individually.
-
Always read the error log carefully.
-
Example error:
Line 7, Column 22: Open quote is expected
➤ Indicates a missing
"
, easy to miss. -
Reading logs helps you learn and debug independently, rather than relying on forums.
Scenario | Solution |
---|---|
Exclude Mobile tests | |
Exclude API tests | |
Run all in a package |
-
Ensure naming conventions in your test methods.
-
Discuss within team to enforce consistent prefixes for modules.
-
Greatly simplifies test execution management via TestNG XML.
Upcoming Lecture: TestNG Annotations
-
You'll learn how annotations like
@BeforeMethod
,@AfterClass
, etc. help manage test flow.