简体   繁体   中英

How to ignore/skip complete testng class containing few test methods & BeforeClass & AfterClass methods

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?

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM