有什么方法可以在Junit5的Java测试类中找到@Disabled Tests?

问题描述

junit5的Java测试类中是否可以找到@disabled Tests?我正在使用junit.platform.launcher在我的项目中发现测试用例。我正在创建一个TestPlan并尝试查找已禁用但无法看到任何帮助的测试。

解决方法

禁用测试是一项执行时间功能,因为禁用/启用可能取决于仅在执行期间可用的内容。这就是为什么测试计划中的测试标识符没有显示的原因。

您所能做的就是检查方法或容器类上是否存在注释。注意注释可以是“元存在”,即另一个注释的注释。 https://junit.org/junit5/docs/current/api/org.junit.platform.commons/org/junit/platform/commons/support/AnnotationSupport.html中的某些方法可能对于减少用于检测所有情况的样板代码数量很有用。

,

我通过使用Reflection和annotations类来获取注释来找到解决方案。收到批注列表后,我使用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
                }
            }
        }
    }