TestNG: Running specific group tests using Testng XML file

1.1k views Asked by At

I have a testng xml file that is setup to run a specific class that has methods containing the following group tags or are grouped as one of the following:

  • parallel-test
  • sequential-test
  • smoke-test

Although some of my tests are designated as either parallel or sequential, some of the test may contain an additional tag 'smoke-test.' In this scenario, I want to be able to just run those that are grouped as 'smoke-test.' When I run the tests, it either cannot find any tests or it just runs all tests grouped as 'sequential-test.' I cannot seem to restrict the test run from this xml file to just those tests grouped as 'smoke-test.' In the same class, I have multiple testNG methods that have either the 'parallel-test' group or the 'sequential-test' group. What it looks like the TestNG xml file is doing is just ignoring the 'parallel-test' group. Thats great, but what if there is Testng method that contains the group 'parallel-test' and 'smoke-test?' I want to run every test regardless of whether it is for parallel testing or sequential, but ONLY those that have the additional group tag 'smoke-test.' My environment is the following:

  • IntelliJ
  • Maven
  • TestNG
  • Java

Please help. See sample test method in the WebTest.Test class:

@Test(groups = {"sequential-test","smoke-test"},description = "Test will validate something")
public void RunTest()
{
    //do something

}

Here is the sample xml file below

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<!--This is a template to be modified for QA environment tests-->
<suite name="Smoke Test">
    <listeners>
        <listener class-name="Listeners.Listeners"/>
        <listener class-name="Listeners.ExtentListeners"/>
    </listeners>
    <parameter name="environment" value="QA"></parameter>
    <test verbose="2" name="Sequential tests" parallel="methods" thread-count="1">
        <!--<groups>
            <run>
                <exclude name="parallel-test"/>
            </run>
        </groups>-->
        <classes>
            <class name="WebTests.Test">
                <methods>
                    <include name="smoke-test"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>
1

There are 1 answers

0
Rahul L On

Below XML Suite should run the all test method having group 'smoke-test'

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<!--This is a template to be modified for QA environment tests-->
<suite name="Smoke Test">
    <listeners>
        <listener class-name="Listeners.Listeners"/>
        <listener class-name="Listeners.ExtentListeners"/>
    </listeners>
    <parameter name="environment" value="QA"></parameter>
    <test verbose="2" name="Sequential tests" parallel="methods" thread-count="1">
       <groups>
            <run>
                <include name="smoke-test"/>
            </run>
        </groups>
        <classes>
            <class name="WebTests.Test"/>

        </classes>
    </test>
</suite>