[英]Is there any way to find @Disabled Tests in a class of Java test class in Junit5?
有沒有辦法在 Junit5 的 Java 測試類中找到 @Disabled 測試? 我正在使用 junit.platform.launcher 來發現我項目中的測試用例。 我正在創建一個 TestPlan 並試圖找到被禁用但看不到任何幫助我的測試。
禁用測試是一個執行時間功能,因為禁用/啟用可能取決於僅在執行期間可用的內容。 這就是測試計划中的測試標識符不顯示的原因。
您所能做的就是檢查方法或容器類上是否存在注釋。 請注意,注釋可以是“元存在”,即另一個注釋的注釋。 https://junit.org/junit5/docs/current/api/org.junit.platform.commons/org/junit/platform/commons/support/AnnotationSupport.html中的一些方法可能有助於減少樣板代碼的數量檢測所有情況。
我通過使用反射和注釋類來獲取注釋找到了解決方案。 收到注釋列表后,我使用 if 條件來檢查測試用例中是否存在任何 Disabled 標記。
`Class clazz = Class.forName (className);
Method[] m = clazz.getDeclaredMethods ();
for(Method method : m){
if(method.getName ().equalsIgnoreCase (methodName)) {
Annotation[] annotations = method.getAnnotations ();
for(Annotation anno : annotations) {
if(anno.annotationType ().getName ().equalsIgnore("org.junit.jupiter.api.Disabled")){
// Do Something
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.