如何配置gradle来输出执行的测试总数?

时间:2016-05-11 21:01:25

标签: testing logging gradle configuration console

当测试正在运行时,到目前为止运行的测试次数是短暂显示的,但是如何在所有测试运行后打印运行到控制台的测试总数?

配置testLogging没有帮助。我可以让gradle输出每个测试的结果,如下所示:

testLogging {
    events "passed", "skipped", "failed"
}

但我想要一个摘要"底线",它输出已运行的测试总数,即使它们都已通过

3 个答案:

答案 0 :(得分:10)

您可以将afterSuite closureTestResult参数一起使用。 F.E. (借鉴https://gist.github.com/orip/4951642):

test {
  testLogging {
    afterSuite { desc, result ->
      if (!desc.parent) { // will match the outermost suite
        println "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)"
      }
    }
  }
}

答案 1 :(得分:2)

将Gradle 2.12与一个简单的项目(带有2个测试套件)一起使用,这个脚本:

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    testCompile 'junit:junit:4.12'
}

def numTests = 0

test {
    beforeTest { descriptor ->
        logger.lifecycle("Running test: " + descriptor)
        numTests++
    }
}

test << {
    println "\nnumTests executed: ${numTests}"
}

给出了这个输出(对我来说):

bash$ gradle clean test
:clean
[snip]
:test
Running test: Test test1(net.codetojoy.ActorTest)
Running test: Test test2(net.codetojoy.ActorTest)
Running test: Test test3(net.codetojoy.ActorTest)
Running test: Test test4(net.codetojoy.ActorTest)
Running test: Test test1(net.codetojoy.SniffTest)
Running test: Test test2(net.codetojoy.SniffTest)
Running test: Test test3(net.codetojoy.SniffTest)
Running test: Test test4(net.codetojoy.SniffTest)
Running test: Test test5(net.codetojoy.SniffTest)
Running test: Test test6(net.codetojoy.SniffTest)

numTests executed: 10

答案 2 :(得分:0)

这是我基于@Hubbitus的答案和this answer

的Android解决方案
subprojects {
    afterEvaluate { project ->
        if (project.plugins.findPlugin('android') ?: project.plugins.findPlugin('android-library')) {
            tasks.withType(Test) {
                testLogging {
                    events "started", "passed", "skipped", "failed"

                }
                afterSuite { desc, result ->
                    if (!desc.parent) { // will match the outermost suite
                        println "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)"
                    }
                }
            }
        }
    }
}

哪个在我的根目录build.gradle中