如何在TestNG中排除类?

时间:2012-08-28 10:59:54

标签: testng

这可以排除testng.xml中的类吗?

我试过

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

但它给出了错误。

8 个答案:

答案 0 :(得分:7)

它的工作原理如下:

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

name属性中,提供包名称。

注意:在TestNG 6.14.3中,您无法排除<classes> XML标记中的类。

答案 1 :(得分:7)

根据TestNG dtdexclude元素仅适用于以下元素:

  • package - 包列表中的包描述。
  • methods - 要包含/排除此测试的方法列表。
  • run - 用于定义应运行哪些组的组的子标签。

不能直接排除元素classesclass;但是,您可以通过组排除类:

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

  public void testMethod1() {
  }

  public void testMethod2() {
  }

}

然后你将定义testng.xml:

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

在大多数情况下,您定义要为运行包含哪些类,而不是排除,因此只需包含您要运行的类。

答案 2 :(得分:2)

在您不想运行的测试后立即添加(groups = { "anyName"}),因此它将如下所示:

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

而在 xml文件中,只需在test name="..."

之后添加
 <groups>
        <run>
            <exclude name="group1" />
        </run>
    </groups>

(groups = { "group1"})的方法不会被运行。 这种方式适合我。请记住,您不能排除类,只能打包,方法和运行。祝你好运

答案 3 :(得分:1)

通过testng.xml没有直接跳过测试类的方法。但是,有一些解决方法可以用来忽略特定的测试类。

  1. 将测试类声明为抽象。
  2. 实现IAnnotationTransformer侦听器接口,并将enabled = false设置为标记有自定义注释的特定类中的所有测试方法。
  3. 在testng-user组中讨论的相同主题但没有直接答案参考邮件线程 - Re:[testng-users]在testng中忽略一个类

答案 4 :(得分:1)

您可以使用classfilesetref并定义要运行的类列表。

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

<testng [...]>
    <!-- your setting here -->
    <classfilesetref refid="test.classes"/>
</testng>

答案 5 :(得分:1)

我有同样的问题!无论如何,我发现的解决方案是使用标记类而不是包这是我的testng.xml文件:

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

在这种情况下,只执行类BrmsServicesFactoryTest测试,其他类测试未执行!

我希望这可以帮到你!

答案 6 :(得分:0)

作为解决方法,您可以通过注释转换器为名称,等于测试方法或测试类的每个测试添加伪组

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);
}

}

并在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>

答案 7 :(得分:0)

如果您正在使用xml文件(testng.xml)定义套件,并且自TestNG 6.14.3起,您不能在XML标记中排除类,则在包中排除特定类的方法如下:

<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>

实际上,您不排除类(dtd不允许它),而是排除了类的所有方法。