3

I'm introducing a Github actions pipeline to an existing project to run ./gradlew test. Unsurprisingly, I've run into cases where tests pass locally but not on the build machine, due to various things like mismatching timezones.

By default, gradle doesn't print the stdout for these tests. I am aware that it will do so if passed --info, however the test suite is some 1500 tests in size which makes the pipeline output extremely verbose (it actually makes my browser lag if I turn it on for the full suite and try to view the resulting output in Github).

To fix the initial teething problems, I've resorted to also targeting the suites that are failing (e.g. ./gradlew test --tests "foo.bar.AppTest" --info). This is a bit of a faff, though. Is there a way to tell gradle to print the stdout contents just for tests that have failed? This would put me in a much better position going forward!

Alyssa
  • 835
  • 1
  • 7
  • 23

2 Answers2

4

This page contains what you are looking for.

It boils down to configuring the test task like so:

test {
  testLogging {
    // set options for log level LIFECYCLE
    events "failed"
  }
}

There are more options to finely control logging if you read that page.


Since you probably only need this for github actions, you can use the CI environmental variable to enable your configurations on CI environments only:

test {
  doFirst {
    if (System.getenv('CI')) {
      testLogging {
        // set options for log level LIFECYCLE
        events "failed"
      }
    }
  }
}

Other CI providers also set this environmental variable

smac89
  • 39,374
  • 15
  • 132
  • 179
  • Thanks! I ended up going with the following, which at least gets CI to print out the failed assertion (e.g. expected X to be Y but was Z). info.events = ["failed", "skipped"] I run tests locally through IntellIJ so didn't opt for the 'CI' env var approach in the end, though this does sound neat! – Alyssa May 04 '20 at 11:28
0

As mentioned in this related answer when dealing with a multi-module android app the following can be used (root build.gradle)

// Call from root build.gradle
setupTestLogging()

fun Project.setupTestLogging() {
    for (sub in subprojects) {
        sub.tasks.withType<Test> {
            testLogging {
                exceptionFormat = TestExceptionFormat.FULL
            }
        }
    }
}

(note that while exceptionFormat alone should be enough to get the wanted outcome, the events("standardOut" ...) mentioned above can be specified in the same way).

For mono-module android projects the same solution will work by dropping the part that iterates on the submodules

Marino
  • 800
  • 1
  • 12
  • 26