如何在TestNG中排除类?
这是否可以排除testng.xml中的类 我试过了如何在TestNG中排除类?,testng,Testng,这是否可以排除testng.xml中的类 我试过了 <packages> <package exclude="com.tt.ee"/> </packages> 但它给出了错误。它的工作原理如下: <packages> <package name="some.package"> <exclude name="some.package.to.exclude"></exclude>
<packages>
<package exclude="com.tt.ee"/>
</packages>
但它给出了错误。它的工作原理如下:
<packages>
<package name="some.package">
<exclude name="some.package.to.exclude"></exclude>
</package>
</packages>
在name
属性中,提供包名称
注意:在TestNG 6.14.3中,不能排除
XML标记中的类。根据,排除元素仅适用于以下元素:
- 包-包列表中的包说明
- 方法-此测试中包括/排除的方法列表
- 运行-用于定义应运行哪些组的组的子标记
不能直接排除类
和类
元素;但是,可以通过组排除类:
@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>
在大多数情况下,您定义了运行时要包含哪些类,而不是要排除哪些类,因此只需包含要运行的类。没有直接的方法可以通过testng.xml跳过测试类。然而,有一些变通方法可以用来忽略特定的测试类
<fileset id="test.classes" dir="${test.classes.dir}" casesensitive="yes">
<exclude name="**/ExcludeClass.java" />
</fileset>
<testng [...]>
<!-- your setting here -->
<classfilesetref refid="test.classes"/>
</testng>
将测试类声明为抽象类李>
实现IAnnotationTransformer侦听器接口,并将enabled=false设置为使用自定义注释标记的特定类中的所有测试方法
testng用户组中讨论的相同主题,但没有直接答案,请参阅邮件线程-Re:[testng用户]忽略testng中的类您可以使用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>
在您不想运行的测试之后添加(groups={“anyName”})
,因此它看起来像:
@Test(groups = { "group1"})
public void methodTestName(){..
}
而不是在您的xml文件中在testname=“…”
具有(groups={“group1”})
的方法将不会运行。
这种方法对我很有效。请记住,不能排除类,只能排除包、方法和运行。祝你好运,我也有同样的问题!无论如何,我找到的解决方案是使用标记类而不是包这是我的testng.xml文件:
<classes>
<class name="brms.impl.BrmsServicesFactoryTest" />
<!-- <class name="brms.impl.ServicesFactoryTest" /> -->
</classes>
在这种情况下,只执行类BRMServicesFactoryTest测试,而不执行其他类测试
我希望这能帮助你 作为解决方法,您可以通过注释转换器将伪组添加到每个名为、等于测试方法或测试类的测试中
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);
}
public类TestNGAnnotationTransformer实现了一个annotationtransformer{
@凌驾
公共void转换(ITestAnnotation注释、类testClass、构造函数testConstructor、方法testMethod){
if(testMethod==null | | annotation==null){
返回;
}
字符串pseudoGroupClass=testMethod.getDeclaringClass().getSimpleName();
字符串pseudoGroupMethod=pseudoGroupClass+““+testMethod.getName();
字符串[]existingGroups=annotation.getGroups();
字符串[]扩展组;
if(existingGroups==null){
extendedGroups=新字符串[]{pseudoGroupClass,pseudoGroupMethod};
}否则{
List tmp=new ArrayList();
用于(字符串组:现有组){
tmp.add(组);
}
tmp.add(伪组类);
tmp.add(伪分组法);
extendedGroups=tmp.toArray(新字符串[0]);
}
注释.setGroups(扩展组);
}
}
并在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>
如果您使用xml文件(testng.xml)来定义a套件,并且由于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不允许),而是排除类的所有方法。答案是-@JayBose excluding与禁用不同。当测试被排除时,它仍然可以在不同的配置中执行。禁用的测试将不会对任何配置执行。