How to incorporate and remove test methods from execution from a
collection of test cases in TestNG?


We can incorporate and remove test methods from execution with the help of <groups> tags in testng xml file.

Example

Testng xml file.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Tutorialspoint Test ">
   <test name = "Test Cycle">
      <groups>
         <run>
            <include name = "Smoke"/>
            <exclude name = "CodingModule"/>
         </run>
      </groups>
      <classes>
         <class name = "TestRegularExpression" />
      </classes>
   </test>
</suite>

The testNG xml has groups Smoke to be included and CodingModule to be excluded from the execution.

Example

@Test(groups={"Smoke"})
public void ContactDetails(){
   System.out.println(“Contact details verification is successful”);
}
@Test(groups={"CodingModule"})
public void verifyInvoice(){
   System.out.println(“Invoice verification is successful”);
}

In a Java class file, groups are used to pinpoint the test method. The test methods with groups as Smoke will be added in the run and CodingModule will be excluded from execution. So ContactDetails() test method shall run but verifyInvoice() shall be left out from execution.

Updated on: 11-Jun-2020

235 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements