How to exclude class in TestNG?

后端 未结 8 1116
囚心锁ツ
囚心锁ツ 2020-12-30 05:17

Is this possible to exclude classes in testng.xml?

I tried with


    



        
相关标签:
8条回答
  • 2020-12-30 05:45

    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

    0 讨论(0)
  • 2020-12-30 05:49

    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>
    
    0 讨论(0)
  • 2020-12-30 05:52

    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.

    0 讨论(0)
  • 2020-12-30 05:53

    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.

    0 讨论(0)
  • 2020-12-30 05:53

    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

    0 讨论(0)
  • 2020-12-30 06:00

    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>
    
    0 讨论(0)
提交回复
热议问题