在完成涉及测试的gradle任务后,如何显示所有测试失败的列表

时间:2017-05-11 14:04:18

标签: testing gradle

在运行包含多模块项目测试的gradle任务后,我希望看到所有模块中每个测试失败的摘要,例如

module 1:

testmodule1thing1 PASSED
testmodule1thing2 FAILED

results
2 tests 1 passed 1 failed

module 2:
testmodule2thing1 PASSED
testmodule2thing2 FAILED

results
2 tests 1 passed 1 failed

module 3:
testmodule3thing1 FAILED

results
1 tests 1 passed 1 failed

BUILD FAILED

========= I already have everything above this line

test failures:
testmodule1thing1
testmodule2thing2
testmodule3thing1

========= I want everything between the last line and this line

这可能吗?如果是这样,怎么样?如果无法获得完整的任务摘要,我可以使用每个模块的摘要

2 个答案:

答案 0 :(得分:2)

您可以将testlistener与buildFinished钩子结合使用。一个非常简单的解决方案可能看起来像这个初稿:

allprojects {
    // add a collection to track failedTests
    ext.failedTests = []

    // add a testlistener to all tasks of type Test
    tasks.withType(Test) {
        afterTest { TestDescriptor descriptor, TestResult result ->
            if(result.resultType == org.gradle.api.tasks.testing.TestResult.ResultType.FAILURE){
                failedTests << ["${descriptor.className}::${descriptor.name}"]
            }
        }
    }

    // print out tracked failed tests when the build has finished
    gradle.buildFinished {
        if(!failedTests.empty){
            println "Failed tests for ${project.name}:"
            failedTests.each { failedTest ->
                println failedTest
            }
            println ""
        }
    }
}

为失败的测试提供更好的可见性的另一个选择是使用gradle构建扫描(https://plugins.gradle.org/plugin/com.gradle.build-scan)。

答案 1 :(得分:1)

这也可以直接在 test 任务中完成:

tasks.withType(Test) {
    // a collection to track failedTests
    ext.failedTests = []

    afterTest { descriptor, result ->
        if (result.resultType == TestResult.ResultType.FAILURE) {
            String failedTest = "${descriptor.className}::${descriptor.name}"
            logger.debug("Adding " + failedTest + " to failedTests...")
            failedTests << [failedTest]
        }
    }

    afterSuite { suite, result ->
        if (!suite.parent) { // will match the outermost suite
            // logs each failed test
            if (!failedTests.empty) {
                logger.lifecycle("Failed tests:")
                failedTests.each { failedTest ->
                    logger.lifecycle("${failedTest}")
                }
            }
        }
    }
}