Executing the Testcases at Package level with regex - rohitkrbhardwaj09/TestNG_ByRSA GitHub Wiki

📌 TestNG XML: Using Regular Expressions for include and exclude

✅ Problem Statement:

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.


🧠 Solution: Use Regular Expressions in exclude (or include) Tags

♻ Naming Convention is Key

  • 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.
    

⚙️ Exclude Using RegEx in TestNG XML

<methods>
  <exclude name="mobile.*" />
</methods>
  • mobile.* will match all test methods starting with mobile

  • .* means any character repeated 0 or more times

  • Result: All methods starting with "mobile" will be excluded from execution


💡 Real-Time Use Case

  • 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.*" />
    

🔄 Swapping Modules

  • To exclude API tests:

    <exclude name="api.*" />
    

🥪 Testing Demonstration Summary

  1. Initially, 7 test cases run.

  2. Added 3 mobile tests: mobileSignIn, mobileSignOut, etc.

  3. After applying <exclude name="mobile.*" />:

    • Total test count still 7

    • The 3 mobile test cases are ignored

  4. Changed to <exclude name="api.*" />:

    • API test cases now excluded

    • Mobile test cases included again


🛆 Running Tests at Package Level

🔍 Why?

  • When running regression tests, you want to execute all tests in a package, regardless of individual class or method.

✅ Package-Level Test Suite

<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.


🛠️ Debugging TestNG XML Errors

  • 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.


🥪 Use Case Summary

Scenario Solution
Exclude Mobile tests
Exclude API tests
Run all in a package

🚀 Real-World Recommendation

  • Ensure naming conventions in your test methods.

  • Discuss within team to enforce consistent prefixes for modules.

  • Greatly simplifies test execution management via TestNG XML.


🔮 Next Topic Preview

Upcoming Lecture: TestNG Annotations

  • You'll learn how annotations like @BeforeMethod, @AfterClass, etc. help manage test flow.

⚠️ **GitHub.com Fallback** ⚠️