17

Is this possible to exclude classes in testng.xml?

I tried with

<packages>
    <package exclude="com.tt.ee"/>
</packages>

but it's giving error.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user522170
  • 623
  • 1
  • 6
  • 21
  • 1
    The answer is here - http://stackoverflow.com/questions/6209177/how-to-disable-entire-unit-test-in-testng – Jay Bose Jun 18 '13 at 20:11
  • 1
    @JayBose Excluding is not the same as disabling. When test is excluded, it still can be executed in a different configuration. Disabled test will not be executed for any configuration. – artdanil Dec 08 '17 at 20:04

8 Answers8

10

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.

artdanil
  • 4,952
  • 2
  • 32
  • 49
8

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.

Nathan
  • 8,093
  • 8
  • 50
  • 76
Tarken
  • 2,112
  • 2
  • 23
  • 42
8

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.

user3582348
  • 123
  • 1
  • 4
2

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

Syeda Zunaira
  • 5,191
  • 3
  • 38
  • 70
Shell Scott
  • 1,679
  • 18
  • 28
1

There is no direct way to skip test class though testng.xml. However there are workaround which can used to ignore particular test class.

  1. Declare the test class as abstract.
  2. Implement IAnnotationTransformer listener interface and set enabled = false to all test methods in a particular class marked with your custom annotation.

The same topic discussed in testng-user group but no direct answer refer the mail thread - Re: [testng-users] Ignore a class in testng

1

You could use classfilesetref and define the list of classes you want to run.

<fileset id="test.classes" dir="${test.classes.dir}" casesensitive="yes">
    <exclude name="**/ExcludeClass.java" />
</fileset>

<testng [...]>
    <!-- your setting here -->
    <classfilesetref refid="test.classes"/>
</testng>
MadDogg
  • 21
  • 2
1

I had the same issue ! Anyway, the solution i found is using the tag classes instead of packages this is my testng.xml file :

    <classes>
        <class name="brms.impl.BrmsServicesFactoryTest" />
<!--            <class name="brms.impl.ServicesFactoryTest" /> -->
    </classes>

in this case, only the class BrmsServicesFactoryTest tests are executed, the other class tests not executed !

I hope this could help you !

Nissrine Nakiri
  • 101
  • 1
  • 7
0

As workaround, you can add pseudo groups to the each test with name, equals test method or test class via annotation transformer

public class TestNGAnnotationTransformer implements IAnnotationTransformer {

@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
    if (testMethod == null || annotation == null) {
        return;
    }

    String pseudoGroupClass = testMethod.getDeclaringClass().getSimpleName();
    String pseudoGroupMethod = pseudoGroupClass + "." + testMethod.getName();

    String[] existingGroups = annotation.getGroups();
    String[] extendedGroups;
    if (existingGroups == null) {
        extendedGroups = new String[] { pseudoGroupClass, pseudoGroupMethod };
    } else {
        List<String> tmp = new ArrayList<String>();
        for (String group : existingGroups) {
            tmp.add(group);
        }
        tmp.add(pseudoGroupClass);
        tmp.add(pseudoGroupMethod);
        extendedGroups = tmp.toArray(new String[0]);
    }

    annotation.setGroups(extendedGroups);
}

}

and use it in your testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="My tests">
    <test name="tests" enabled="true">
        <groups>
            <run>
                <include name="My Group"/> 
                <include name="MySuperClass"/>
                <exclude name="MySuperClass.badMethod"/>
                <exclude name="DontLikeThisClassAnymore"/>
            </run>
        </groups>

        <packages>
            <package name="com.mycompany.*"/>
        </packages>
    </test>

    <listeners>
       <listener class-name="com.mycompany.TestNGAnnotationTransformer"/>
    </listeners>
</suite>