简体   繁体   English

Gradle 测试 - 为失败的测试打印标准输出/标准错误

[英]Gradle test - print stdout/stderror for failed tests

I'm introducing a Github actions pipeline to an existing project to run ./gradlew test .我正在将 Github 操作管道引入现有项目以运行./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.默认情况下,gradle 不会打印这些测试的stdout 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).我知道如果通过--info它将这样做,但是测试套件的大小约为 1500 个测试,这使得管道 output 非常冗长(如果我为完整套件打开它并尝试在 Github 中查看生成的 output)。

To fix the initial teething problems, I've resorted to also targeting the suites that are failing (eg ./gradlew test --tests "foo.bar.AppTest" --info ).为了解决最初的初期问题,我还采用了针对失败的套件(例如./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?有没有办法告诉 gradle 仅为失败的测试打印标准输出内容? This would put me in a much better position going forward!这将使我在一个更好的 position 前进!

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:由于您可能只需要 github 操作,因此您可以使用CI环境变量仅在 CI 环境中启用您的配置:

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

Other CI providers also set this environmental variable 其他CI 提供者也设置了这个环境变量

As mentioned in this related answer when dealing with a multi-module android app the following can be used (root build.gradle )正如在处理多模块 android 应用程序时在此相关答案中提到的,可以使用以下内容(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). (请注意,虽然exceptionFormat本身就足以获得想要的结果,但上面提到的events("standardOut"...)可以以相同的方式指定)。

For mono-module android projects the same solution will work by dropping the part that iterates on the submodules对于单模块 android 项目,相同的解决方案将通过删除在子模块上迭代的部分来工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM