top of page

Include And Exclude In TestNG

What Is Include And Exclude In TestNG ?

TestNg has a feature to include and exclude the test methods,classes and packages using include and exclude tag in .xml

To include some test methods of a class instead of all test methods,use include tag with that particular test method name :

<include name="test1"></include>

And for exclusion of test method exclude tag is used or set value false for attribute enabled :

<exclude name="test1"></exclude>

OR

@Test(enabled = false)   public void test2() {

        }

Here's an example of it :

  • Create a testNG class as Include_ExcludeExample.

  • Create a .xml file.

package testNGexamples;

import org.testng.annotations.Test;

public class Include_ExcludeExample {


  @Test
  public void test1() {
     System.out.println("test1 is executed ");
  }


  @Test
  public void test2() {
     System.out.println("test2 is executed ");
  }
 
}

 

.xml file for include :

 

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test-Suite">
   <test name="Group Test">
       <classes>
           <class name="testNGexamples.Include_ExcludeExample">
               <methods>
                   <include name="test1"></include>
               </methods>
           </class>
       </classes>
   </test>
</suite>

Output :

[RemoteTestNG] detected TestNG version 7.4.0
test1 is executed 

===============================================
Test-Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

.xml file for exclude :

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test-Suite">
   <test name="Group Test">
       <classes>
           <class name="testNGexamples.Include_ExcludeExample">
               <methods>
                   <exclude name="test1"></exclude>
               </methods>
           </class>
       </classes>
   </test>
</suite>

 

Output :

[RemoteTestNG] detected TestNG version 7.4.0
test2 is executed 

===============================================
Test-Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

 

TestNG example to disable a test :

package testNGexamples;

import org.testng.annotations.Test;

public class EnabledAttributeOfTest {


   @Test
   public void test1() {
       System.out.println("test1 is executed ");
   }

    // To disable a test
   @Test(enabled = false)
   public void test2() {
       System.out.println("test2 is executed ");
   }
}

 

 

Output :

[RemoteTestNG] detected TestNG version 7.4.0
test1 is executed 
PASSED: test1

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

Refer next page TestNG Group

bottom of page