生成测试以正确报告结果

时间:2012-11-29 21:21:05

标签: java junit intellij-idea

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

这可以正确地运行测试,任何数量的需求和报告都可以用于不需要任何其他测试的测试,但是当我运行需要另一个的测试时,我得到“完成:1的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;
}

1 个答案:

答案 0 :(得分:2)

您的自定义跑步者应该预先定义org.junit.runners.ParentRunner#getDescription,然后IDEA会正确显示树。

相关问题