How to exclude class in TestNG?

It works like this:

<packages>
    <package name="some.package">
        <exclude name="some.package.to.exclude"></exclude>
    </package>
</packages>

In the name attributes, provide the package names.

Note: In TestNG 6.14.3, you cannot exclude classes in the <classes> XML tag.


According to the TestNG dtd, the exclude element is only applicable to the following elements:

  • package - The package description within packages list.
  • methods - The list of methods to include/exclude from this test.
  • run - The subtag of groups used to define which groups should be run.

The elements classes and class cannot be directly excluded; however, you can exclude classes through groups:

@Test(groups = { "ClassTest1" })
public class Test1 {

  public void testMethod1() {
  }

  public void testMethod2() {
  }

}

Then you will define the testng.xml:

<suite>
  <test>
    <groups>
      <run>
        <exclude name="ClassTest1"/>
      </run>
    </groups>
    <classes>
      <class name="Test1">
    </classes>
  </test> 
</suite>

In most cases you define, which classes to include for the run, not to exclude, so just include the classes you want to run.


Add (groups = { "anyName"}) right after tests you don't want to run, so it will be look like:

 @Test(groups = { "group1"})
public void methodTestName(){..
}

And than in your xml file add just after test name="..."

 <groups>
        <run>
            <exclude name="group1" />
        </run>
    </groups>

Methods with (groups = { "group1"}) wont be runned. This way works for me. Remember that you can't exclude Class, only package, methods and runs. Good luck


If you are using a xml file (testng.xml) to define the a suite, and since TestNG 6.14.3, you cannot exclude classes in the XML tag, a way to exclude a specific class inside a package is the following:

<suite name="suite-name" >
<test name="test-name">
    <packages>
        <package name="ab.cd.ef.*"/>
    </packages>
    <classes>
        <class
            name="ab.cd.ef.ClassToBeExcluded">
            <methods>
                <exclude name=".*" />
            </methods>
        </class>
    </classes>
</test>

Effectively, instead of excluding the class (dtd does not allow it), all the methods of the class are excluded.

Tags:

Testng