使用spock junit测试和gradle构建系统在控制台上输出unit testing结果

GNU Emacs 24.3.1 Gradle 1.12 spock-core.0.7 

你好,

我正在使用gradle build系统使用spock framework进行unit testing。

当我运行我的测试gradle test我只看到这样的消息:

 * What went wrong: Execution failed for task ':test'. > There were failing tests. See the report at: file:///home/projs/gradleTest/build/reports/tests/index.html 

然后我必须去检查xml文件以查看抛出了什么exception

有没有我可以在控制台输出中看到exception的选项? 例如,我有一些打印消息,我想打印到控制台输出:

  catch(FileNotFoundException ex) { System.out.println("Exception " + ex.toString()); } catch(IOException ex) { System.out.println("Exception " + ex.toString()); } catch(Exception ex) { System.out.println("Exception " + ex.toString()); } 

我的Gradle构建文件:

 apply plugin: 'java' apply plugin: 'application' apply plugin: 'groovy' repositories { mavenLocal() mavenCentral() } dependencies { compile "com.googlecode.json-simple:json-simple:1.1.1" testCompile "org.codehaus.groovy:groovy:2.3.3" testCompile "org.spockframework:spock-core:0.7-groovy-2.0" } 

我的spockunit testing:

 import spock.lang.Specification; class SnapClientTest extends Specification { def "Request from web services"() { given: SnapClient snapclient = new SnapClient() expect: snapclient.makeRequest() == 0 } } 

提前谢谢了,

当我运行我的测试gradle测试时,我只看到这样的消息:[…]

默认情况下,您还会看到(在输出中更进一步):

  • 哪种测试方法失败了
  • exception类型
  • 发生exception的源文件和行号

要查看更多信息,请配置Test#testLogging 。 例如:

 tasks.withType(Test) { testLogging { exceptionFormat "full" } } 

有关更多详细信息,请在Gradle Build语言参考中选中Test

然后我必须去检查xml文件以查看抛出了什么exception

通常,您将检查HTML报告,而不是XML文件。 在Mac上,只需按住CMD并在See the report at:后双击文件URL即可打开See the report at: 。 一些* nix终端提供类似的function。

尝试gradle --stacktrace test 。 我相信这将显示产生测试失败/错误的堆栈跟踪。

您也可以添加--debug选项以获取其他信息。