Gradle测试 – 打印失败的测试的标准输出/标准错误信息

huangapple go评论58阅读模式
英文:

Gradle test - print stdout/stderror for failed tests

问题

我正在向现有项目引入Github Actions流水线,以运行./gradlew test。毫不奇怪,我遇到了一些情况,即在本地测试通过,但在构建机器上却未能通过,原因是各种各样的问题,如时区不匹配。

默认情况下,gradle不会打印这些测试的stdout。我知道如果传递了--info,它会这样做,但是测试套件有大约1500个测试,这使得流水线输出非常冗长(如果我为整个套件打开它并尝试在Github中查看生成的输出,实际上会使我的浏览器延迟)。

为了解决最初的问题,我也尝试针对失败的测试套件(例如./gradlew test --tests "foo.bar.AppTest" --info)。虽然这有点麻烦,但是否有办法告诉gradle仅为未通过的测试打印stdout内容?这将使我在今后的工作中处于更好的位置!

英文:

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!

答案1

得分: 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:

英文:

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

答案2

得分: 0

在这个相关答案中提到的,在处理多模块Android应用程序时,可以使用以下内容(根build.gradle

// 从根build.gradle调用
setupTestLogging()

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

(请注意,尽管仅使用exceptionFormat应足以获得所需的结果,但上述提到的events("standardOut"...)也可以以相同的方式指定)。

对于单模块Android项目,通过删除迭代子模块的部分,相同的解决方案也将起作用。

英文:

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&lt;Test&gt; {
            testLogging {
                exceptionFormat = TestExceptionFormat.FULL
            }
        }
    }
}

(note that while exceptionFormat alone should be enough to get the wanted outcome, the events(&quot;standardOut&quot; ...) 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

huangapple
  • 本文由 发表于 2020年5月4日 03:49:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/61580713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定