0

我创建了一个自定义的 JUnit 4 运行器(扩展 BlockJUnit4Runner),其目的是运行其他测试在运行这些测试之前有效所必需的测试。例如,如果您正在测试某个文件 IO,则读/写测试将需要打开/关闭测试才能正常工作。

这可以使用任意数量的要求正确运行测试,并且对于不需要其他任何东西的测试返回正常报告,但是当我运行一个确实需要另一个测试时,我得到“完成:1 of 2”,并且事件日志说“测试通过:1 通过”。即使要求的测试(第二个运行)失败也是如此。

@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
    if (!alreadyRunMethods.containsKey(method.getName())) {
        boolean requiredMethodsPassed = true;
        for (Annotation anno : method.getAnnotations()) {
            if (anno instanceof Requires) {
                requiredMethodsPassed = runRequiredMethods((Requires) anno, notifier, method);
                break;
            }
        }
        if (requiredMethodsPassed) {
            super.runChild(method, notifier);
        }else{
            notifier.fireTestAssumptionFailed(new Failure(describeChild(method), new AssumptionViolatedException("Required methods failed.")));
        }
    }
}

private boolean runRequiredMethods(Requires req, RunNotifier notifier, FrameworkMethod parentMethod) {
    boolean passed = true;
    for (String methodName : req.value()) {
        FrameworkMethod method = methodByName.get(methodName);
        if (method == null) {
            throw new RuntimeException(String.format("Required test method '%s' on test method '%s' does not exist.", methodName, parentMethod.getName()));
        }
        runChild(method, notifier);
        Boolean methodPassed = alreadyRunMethods.get(method.getName());
        methodPassed = methodPassed == null ? false : methodPassed;
        passed &= methodPassed;
    }
    return passed;
}
4

1 回答 1

2

您的自定义运行器应该预先定义org.junit.runners.ParentRunner#getDescription,然后 IDEA 将正确显示树。

于 2012-11-30T12:26:02.887 回答