Is this possible to exclude classes in testng.xml?
I tried with
<packages>
<package exclude="com.tt.ee"/>
</packages>
but it's giving error.
Is this possible to exclude classes in testng.xml?
I tried with
<packages>
<package exclude="com.tt.ee"/>
</packages>
but it's giving error.
According to the TestNG dtd, the exclude
element is only applicable to the following elements:
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.
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.
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.
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
There is no direct way to skip test class though testng.xml. However there are workaround which can used to ignore particular test class.
The same topic discussed in testng-user group but no direct answer refer the mail thread - Re: [testng-users] Ignore a class in testng
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>
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 !
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>