How to exclude class in TestNG?

2019-02-06 10:19发布

Is this possible to exclude classes in testng.xml?

I tried with

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

but it's giving error.

标签: testng
7条回答
祖国的老花朵
2楼-- · 2019-02-06 10:32

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>
查看更多
地球回转人心会变
3楼-- · 2019-02-06 10:38

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>
查看更多
Melony?
4楼-- · 2019-02-06 10:49

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

查看更多
Bombasti
5楼-- · 2019-02-06 10:50

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.

查看更多
Animai°情兽
6楼-- · 2019-02-06 10:50

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

查看更多
再贱就再见
7楼-- · 2019-02-06 10:53

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 !

查看更多
登录 后发表回答