2016-09-15 50 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

相關問題