1

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?

2 Answers 2

2

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> 
Sign up to request clarification or add additional context in comments.

11 Comments

Hi Julien, but how to skip the class , because while using your implementation, i could not see method such as setEnabled(false); If you can provide exact implementation, that will be helpful
They are not skipped because they are not run at all. But why do you want to to see them skipped (aka "we are not able to run them because something was wrong before")?
This might be because beforeclass & afterclass is also set as alwaysrun = true. Is there any way to skip them as well
Before/AfterClass are not supposed to be run if there is no test in the class and child classes. Please try the latest testng version (6.9.12).
Actually alwaysRun=true for beforeclas & afterclass method, and if I am removing alwaysRun from them , then getting, this error java.lang.NullPointerException at org.testng.DependencyMap.getMethodDependingOn(DependencyMap.java:54) at org.testng.TestRunner.createDynamicGraph(TestRunner.java:1068) at org.testng.TestRunner.privateRun(TestRunner.java:734)
|
-1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.