Reputation: 56
I have testng.xml suppose,
<suite name="TestSuite" parallel="false">
<test name="smoke" preserve-order="true" verbose="2">
<groups>
<run>
<include name="smoke"/>
</run>
</groups>
<classes>
<class name="com.testClass1"/>
<class name="com.testClass2"/>
</classes>
</test>
</suite>
This may contain more classes close to 10-15, this is my generic testng.xml, from different set of testdata, what I want is to skip com.testClass1 classs, for particular case, and rest of test should execute.
I tried with implementing my class using, IAnnotationTransformer listener of testng.
the code snippet is,
public class SkipTestClass implements IAnnotationTransformer{
private SessionProfile profile=null;
public void transform(ITestAnnotation annotation, Class testClass,
Constructor testConstructor, Method testMethod) {
if (testMethod != null) {
Test test = testMethod.getDeclaringClass().getAnnotation(Test.class);
if (test != null && !test.enabled() && testMethod.getDeclaringClass().getClass().getName().equalsIgnoreCase("com.testClass1")) {
annotation.setEnabled(false);
}
}
}
}
and calling this listener at Test Class level,as
@Listeners(com.SkipTestClass.class),
Expected Result: I am assuming, only this class com.testClass1 & it's testmethods & beforeclass & afterclass methods should skip and rest of suite should execute.
Actual Result: Whole suite is getting skipped.
Any Help, please?
Upvotes: 1
Views: 1893
Reputation: 5740
Whole suite is getting skipped.
I suppose it is because the run fails somewhere because your listener looks good. You can set a higher verbose level to check what is happening.
BTW, IMethodInterceptor is a better listener choice because it doesn't depend on annotation which may be or not present on class and/or test.
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
List<IMethodInstance> result = new ArrayList<IMethodInstance>();
for (IMethodInstance m : methods) {
if (m.getDeclaringClass() != testClass1.class) {
result.add(m);
}
}
return result;
}
And prefer to add this listener in the suite description:
<suite name="TestSuite" parallel="false">
<listeners>
<listener class-name="...MyListener"/>
</listeners>
<test name="smoke" preserve-order="true" verbose="2">
<groups>
<run>
<include name="smoke"/>
</run>
</groups>
<classes>
<class name="com.testClass1"/>
<class name="com.testClass2"/>
</classes>
</test>
</suite>
Upvotes: 2
Reputation: 202
You can use suite to exclude/include test cases.
@RunWith(Suite.class)
@Suite.SuiteClasses({
AuthenticationTest.class
/* USERRestServiceTest.class*/
})
public class JunitTestSuite
{
}
and then use the runner
@Category(IntegrationTest.class)
public class TestRunner {
@Test
public void testAll() {
Result result = JUnitCore.runClasses(JunitTestSuite.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
if (result.wasSuccessful()) {
System.out.println("All tests finished successfully...");
}
}
}
More details - TestRunner Documentation
Upvotes: -1