簡體   English   中英

跳過 IAnnotationTransformer TestNG 中的測試或課程

[英]Skip tests or classes in IAnnotationTransformer TestNG

我想用IAnnotationTransformer跳過(所以我會在報告中看到測試/類被跳過),我所做的是像這樣的監聽器 class :

public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {

    System.out.println(testMethod.getDeclaringClass().getName());
    if(testMethod.getDeclaringClass().getName().contains("a12S")){
        System.out.println("was in a12S");
        annotation.setEnabled(false);
    }
    if(testMethod.getDeclaringClass().getName().contains("note5")){
        System.out.println("was in note5");
        annotation.setEnabled(false);
    }
    if(testMethod.getDeclaringClass().getName().contains("onePlus6Pro")){
        System.out.println("was in onePlus 6 pro");
        annotation.setEnabled(false);
    }

}

我已經測試了 class:

@Test(priority = 1)
public void installAndDenyPhoneAccess() {
    onePlus6ProInstallProcess.denyPhoneAccess();
}

testng.xml我添加了監聽器:

    <listeners>
        <listener class-name="com.qa.listeners.TestIannotationTransformer" />
    </listeners>

無論是annotation.setEnabled(false)還是true ,測試開始運行,也不會標記為已跳過。 那么如何跳過呢? 如果可以的話,如何跳過所有 class ?

IAnnotationTransformer用於修改@Test注解。 並且沒有選項可以跳過使用@Test注釋的方法。 為了強行跳過它,您需要拋出一個SkipException

從代碼來看,您似乎需要跳過僅基於 class 名稱的測試,並且方法名稱不相關。 因此,我建議通過覆蓋onTestStart方法來使用ITestListener ,如下所示:

public class MyTestListener implements ITestListener {
    @Override
    public void onTestStart(ITestResult result) {
        String className = result.getTestClass().getName();
        if(className.contains("abc") || className.contains("xyz")) {
            throw new SkipException("Skipping class: " + className);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM