[英]TestNG Re run test on failure, not running BeforeClass AfterClass methods
[英]How to ignore/skip complete testng class containing few test methods & BeforeClass & AfterClass methods
我有testng.xml假設,
<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類,對於特定情況,其余的測試應該執行。
我嘗試使用testng的IAnnotationTransformer監聽器實現我的類。
代碼片段是,
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);
}
}
}
}
並在Test Class級別調用此偵聽器,如
@Listeners(com.SkipTestClass.class),
預期的結果:我假設,只有這個類com.testClass1&它的testmethods&beforeclass和afterclass方法應該跳過,其余的套件應該執行。
實際結果:整個套件被跳過。
請幫忙嗎?
整套房間都被忽略了。
我想這是因為你的聽眾看起來很好,因為跑步失敗了。 您可以設置更高的詳細級別來檢查發生的情況。
順便說一句, 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>
您可以使用套件來排除/包含測試用例。
@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文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.