Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure gradle to output total number of tests executed?

While tests are running, the number of tests run so far is ephemerally displayed, but how can I print the total number of tests that were run to the console after all tests have run?

Configuring testLogging doesn't help. I can make gradle output a result for every test, like this:

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

But I want a summary "bottom line", that outputs the total number of tests that were run, even if they all passed.

like image 647
Bohemian Avatar asked May 11 '16 21:05

Bohemian


People also ask

How do I create a test report in Gradle?

How to generate a Test Report. Gradle generates a Test Report automatically when it runs the entire Test Suite. To do the same, run ./gradlew test (or gradlew. bat test from Windows), or run the test Gradle task from your IDE.

How do I list all tasks in Gradle?

To get an overview of all Gradle tasks in our project, we need to run the tasks task. Since Gradle 5.1, we can use the --group option followed by a group name. Gradle will then show all tasks belonging to the group and not the other tasks in the project.

How does Gradle find tests?

Test detection By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. This detection uses different criteria depending on the test framework used. For JUnit, Gradle scans for both JUnit 3 and 4 test classes.


2 Answers

You may use afterSuite closure with TestResult argument. F.e. (borrowed from 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)"
      }
    }
  }
}
like image 177
Hubbitus Avatar answered Oct 22 '22 08:10

Hubbitus


Using Gradle 2.12 with a simple project (with 2 test suites), this script:

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}"
}

gives this output (for me):

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
like image 42
Michael Easter Avatar answered Oct 22 '22 08:10

Michael Easter