2016-09-15 53 views
0

我的testng.xml想,如何忽略/跳过完结TestNG的类包含一些测试方法及BeforeClass及课余方法

<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> 

这可能包含接近10-15多类,这是我的通用TestNG的.xml,来自不同的testdata集,我想要的是跳过com.testClass1类,特殊情况下,其余测试应该执行。

我尝试着使用我的类,IAnnotationTransformer侦听器的testng。

的代码段,

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); 
         } 

        } 

     } 
    } 

,并调用这个监听器在测试类水平,

@Listeners(com.SkipTestClass.class),

预期结果:我假设,只有这个类com.testClass1 &它的测试方法& beforeclass & afterclass方法应该跳过,套件的其余部分应该执行。

实际结果:整个套件正在跳过。

请帮忙吗?

回答

-1

您可以使用suite来排除/包含测试用例。

@RunWith(Suite.class) 
@Suite.SuiteClasses({ 
         AuthenticationTest.class 
        /* USERRestServiceTest.class*/ 
}) 

    public class JunitTestSuite 
{ 

} 

,然后使用亚军

@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..."); 
     } 
} 
} 

更多细节 - TestRunner Documentation

1

全套房越来越跳过。

我想这是因为你的听众看起来不错,所以运行失败。您可以设置较高的详细级别来检查发生了什么。

顺便说一句,IMethodInterceptor是一个更好的倾听者选择,因为它不依赖于类和/或测试中可能存在或不存在的注释。

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; 
} 

,喜欢在浴室说明添加此监听器:

<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> 
+0

嗨朱利安,但如何跳过级,因为在使用您的实现,我看不出法如的setEnabled(假);如果您可以提供确切的实施,那将是有帮助的 – PrateekSethi

+0

它们不会被跳过,因为它们根本没有运行。但为什么你想看到他们跳过(又名“我们无法运行它们,因为之前有什么不对劲”)? – juherr

+0

这可能是因为beforeclass&afterclass也被设置为alwaysrun = true。是否有任何方法可以跳过它们 – PrateekSethi

相关问题