</>

Technology

TestNG

Difficulty

Intermediate

Interview Question

How do you use testng.xml groups to include and exclude tests in TestNG?

Answer

Groups Include/Exclude in testng.xml

Groups allow you to run only specific categories of tests without changing code.

Define Groups in Test Code

Java
@Test(groups = {"smoke", "login"})
public void validLoginTest() { ... }

@Test(groups = {"regression", "login"})
public void loginWithInvalidEmailTest() { ... }

@Test(groups = {"smoke", "checkout"})
public void addToCartTest() { ... }

@Test(groups = {"regression", "checkout", "payment"})
public void fullCheckoutFlowTest() { ... }

@Test(groups = "wip") // work in progress — exclude from CI
public void newFeatureTest() { ... }

testng.xml — Include Specific Groups

XML
<!-- Run ONLY smoke tests -->
<suite name="SmokeSuite">
    <test name="SmokeTests">
        <groups>
            <run>
                <include name="smoke"/>
            </run>
        </groups>
        <classes>
            <class name="tests.LoginTest"/>
            <class name="tests.CheckoutTest"/>
        </classes>
    </test>
</suite>

testng.xml — Exclude Groups

XML
<!-- Run everything EXCEPT wip and broken groups -->
<suite name="RegressionSuite">
    <test name="AllTests">
        <groups>
            <run>
                <include name="regression"/>
                <exclude name="wip"/>
                <exclude name="broken"/>
            </run>
        </groups>
        <classes>
            <class name="tests.LoginTest"/>
            <class name="tests.CartTest"/>
            <class name="tests.PaymentTest"/>
        </classes>
    </test>
</suite>

Group Definitions — Define Group Hierarchy

XML
<suite name="Suite">
    <test name="Tests">
        <groups>
            <define name="allTests">
                <include name="smoke"/>
                <include name="regression"/>
            </define>
            <run>
                <include name="allTests"/>
                <exclude name="wip"/>
            </run>
        </groups>
    </test>
</suite>

Run via Maven — Override Groups at Runtime

Bash
# Run only smoke tests from command line
mvn test -Dgroups="smoke"

# Run smoke AND login groups
mvn test -Dgroups="smoke,login"

# Exclude wip
mvn test -DexcludedGroups="wip"

Common Group Strategy in Enterprise

Java
// Group naming convention
@Test(groups = {"smoke"})           // Critical path — run on every commit
@Test(groups = {"regression"})      // Full suite — run nightly
@Test(groups = {"api"})             // API-only — fast, no browser needed
@Test(groups = {"ui"})              // Browser tests — slower
@Test(groups = {"wip"})             // In progress — never run in CI
@Test(groups = {"broken"})          // Known defect — document and exclude
@Test(groups = {"performance"})     // Load tests — run weekly

CI/CD Pipeline Group Strategy

GROOVY
// Jenkinsfile
stage('Smoke Tests') {
    steps { sh 'mvn test -Dgroups="smoke"' }
}
stage('Full Regression') {
    when { branch 'main' }
    steps { sh 'mvn test -Dgroups="regression" -DexcludedGroups="wip,broken"' }
}

Follow AutomateQA

Related Topics