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

jgovgodb  于 2023-08-06  发布在  其他
关注(0)|答案(5)|浏览(106)

当测试运行时,暂时显示 * 到目前为止 * 运行的测试数,但是如何在所有测试运行后将运行的测试总数打印到控制台?
配置testLogging没有帮助。我可以让gradle输出 every test的结果,像这样:

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

字符串
但是我想要一个总结的“底线”,输出运行的测试总数,* 即使它们都通过了 *。

6ie5vjzr

6ie5vjzr1#

您可以使用afterSuite闭包和TestResult参数。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)"
      }
    }
  }
}

字符串

fnvucqvd

fnvucqvd2#

将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

x3naxklr

x3naxklr3#

KotlinDSL根据@Hubbitus的接受答案应用答案(比预期的要复杂一些):

tasks.withType<AbstractTestTask> {
    afterSuite(
        KotlinClosure2({ desc: TestDescriptor, result: TestResult ->
            // Only execute on the outermost suite
            if (desc.parent == null) {
                println(" **** Result: ${result.resultType} ****")
                println("  >    Tests: ${result.testCount}")
                println("  >   Passed: ${result.successfulTestCount}")
                println("  >   Failed: ${result.failedTestCount}")
                println("  >  Skipped: ${result.skippedTestCount}")
            }
        })
    )
}

字符串
使用KotlinClosure2允许使用两个预期参数(TestDescriptorTestResult)调用afterSuite Groovy闭包,Unit是闭包的返回类型(参考:从Kotlin调用groovy闭包)。

7dl7o3gd

7dl7o3gd4#

以下是我基于@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)"
                    }
                }
            }
        }
    }
}

字符串
这是我的根建gradle

e5njpo68

e5njpo685#

Gradle使用test-reports编写易于解析的XML文件。例如,您可以使用Kotlin script解析它们:

@file:DependsOn("net.mbonnin.xoxo:xoxo:0.3")

import xoxo.toXmlDocument
import java.io.File

File(".").walk().filter { it.name == "test-results" && it.isDirectory }
    .toList()
    .flatMap {
      it.walk().filter { it.isFile && it.extension == "xml" }.toList()
    }
    .map {
       it.toXmlDocument()
          .root
          .attributes["tests"]!!
          .toInt()
    }
    .sum()

字符串

相关问题