如果我想禁用测试,如果测试方法下方存在某些注释,如何处理 ExecutionCondition?

How to deal with ExecutionCondition if i want to disable test, if some annotation present below the test method?

目前正在尝试 JUnit 5 并希望在我的自动化框架中实现如果我的自定义注释存在则跳过测试的可能性。

如果我想这样做:

public class KnownIssueExtension implements ExecutionCondition {


    @Override
    public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    if(context.getRequiredTestMethod().isAnnotationPresent(KnownIssue.class)){
        return disabled("HERE DISABLED");
//        if(context.getTestMethod().isPresent(element -> findAnnotation(element, KnownIssue.class))){
     } else return enabled("enabled");
    }
}

我收到这样的异常:

org.junit.jupiter.engine.execution.ConditionEvaluationException: Failed to evaluate condition [org.talend.qa.iam.utils.KnownIssueExtension]: Illegal state: required test method is not present in the current ExtensionContext

但另一种方法如:

context.getTestMethod 

不检查注释是否存在。

谁能指出我做错了什么? 提前致谢

我不知道此后 API 是否发生了变化,但我想在最初测试 JUnit 5 时做一些类似的事情。(参见 here。)

抱歉只是代码转储,但它看起来很简单,可能是不言自明的:

public final class KnownIssueExtension implements TestExecutionExceptionHandler {

    @Override
    public void handleTestExecutionException(final TestExtensionContext context,
                                             final Throwable throwable) 
            throws Exception {

        final Method testMethod = context.getTestMethod().get();
        if (!testMethod.isAnnotationPresent(KnownIssue.class)) {
            throw throwable;
        }
    }
}

(注:未经测试,即使编译。)

编辑:


哦,我才注意到跳过,没有忽略失败。在这种情况下,您注释掉的代码之类的东西不起作用:

final Optional<Method> methodOptional = context.getTestMethod();
if (methodOptional.isPresent()
        && methodOptional.get().isAnnotationPresent(KnownIssue.class)) {
    // ...
}

工作解决方案:

public class KnownIssueExtension implements ExecutionCondition {

@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {

    final Optional<Method> methodOptional = context.getTestMethod();
    if (methodOptional.isPresent()
            && methodOptional.get().isAnnotationPresent(KnownIssue.class)) {
        return disabled("DISABLED");
    }
    return enabled("ENABLED");
}

}