在gradle测试期间,请显示标准输出,并且仅在失败的测试中显示错误 [英] During gradle test show standard out and err only for failed tests

查看:119
本文介绍了在gradle测试期间,请显示标准输出,并且仅在失败的测试中显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在travis-ci上运行了一个大型测试套件,该套件具有文本输出.我想以仅针对失败测试的方式配置gradle,以显示标准输出和标准错误流.对于已正确执行的所有其他测试,不应发生这种情况,以使控制台不会被那种噪音污染.

I have a large test suite running on travis-ci, which has a textual output. I would like to configure gradle in a way that only for failed tests, the standard output and standard error streams are displayed. For all other tests which have been executed correctly, this should not happen so that the console is not polluted with that noise.

我知道如何启用或禁用标准输出/错误日志记录,但是我不确定如何使此依赖项取决于测试结果.

I am aware how to enable or disable the standard output/error logging, but I am unsure how to make this dependend on the test result.

推荐答案

可以使用以下gradle配置将其存档

This can be archived with following gradle config

project.test {
  def outputCache = new LinkedList<String>()

  beforeTest { TestDescriptor td -> outputCache.clear() }    // clear everything right before the test starts

  onOutput { TestDescriptor td, TestOutputEvent toe ->       // when output is coming put it in the cache
    outputCache.add(toe.getMessage())
    while (outputCache.size() > 1000) outputCache.remove() // if we have more than 1000 lines -> drop first
  }

  /** after test -> decide what to print */
  afterTest { TestDescriptor td, TestResult tr ->
    if (tr.resultType == TestResult.ResultType.FAILURE && outputCache.size() > 0) {
        println()
        println(" Output of ${td.className}.${td.name}:")
        outputCache.each { print(" > $it") }
    }
  }
}

Git回购: https://github.com/calliduslynx/gradle-log-失败

原始内容在这里: https://discuss .gradle.org/t/show-stderr-for-failed-tests/8463/7

这篇关于在gradle测试期间,请显示标准输出,并且仅在失败的测试中显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆