本文介绍了当规范运行时,如何打印出spock元数据(给定的内容,何时|然后)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能吗?如果是,这将对调试有很大帮助.

Is it possible or not?If yes,it will be a big help for debugging.

更新:对于元数据,我的意思是给定| when | then语句之后的内容,例如:

Update:For metadata,I mean the content of after given|when|then statement,such as:

def "test case"...
given:"some preconditions"
when:"do something"
then:"some result"
...

我希望打印打击内容:

"test case" begins
"some preconditions"
"do something"
"some result"

推荐答案

当前这是不可能的,因为即使您编写了Spock扩展,目前可以挂入的最深层次是要素方法的执行. IE.您可以做的是在执行方法之前或之后打印所有块标签,但不要在执行方法期间穿插自己的日志输出.目前,方法内块执行没有钩子或拦截点.

Currently this is not possible because even if you write a Spock extension, the deepest you can hook into at the moment is feature method execution. I.e. what you could do is print all block labels before or after a method is executed, but not interspersed with your own log output during method execution. There is no hook or interception point for intra-method block execution at the moment.

另请参阅以下功能请求(不幸的是,仍未得到答复):

See also these feature requests (still unanswered, unfortunately):

  • https://github.com/spockframework/spock/issues/538
  • https://github.com/spockframework/spock/issues/645

围绕记录块标签,还可以将HTML测试报告编写为测试文档.但这是一个报告,不是在运行测试期间可以使用的东西.

What is also possible around logging block labels is to write HTML test reports as a test documentation. But this is a reporting thing and not something you can use during a running test.

更新:同时有一些解决方法.将其放入您的全局Spock配置脚本中(通常src/test/resources/SpockConfig.groovy:

Update: Meanwhile a little workaround. Put this in your global Spock config script (usually src/test/resources/SpockConfig.groovy:

import spock.lang.Specification

class LabelPrinter {
  def _(def message) {
    println message
    true
  }
}

Specification.mixin LabelPrinter

然后在这样的Spock或Geb测试中使用它(请注意标签后的下划线):

Then use it from Spock or Geb tests like this (please note the underscores after the labels):

package de.scrum_master.testing

import spock.lang.Specification

class MySpockTest extends Specification {
  def "interaction"() {
    given:_ "My given comment"
    def foo = 11
    def bar = foo + 4
    println "blah"

    expect:_ "My expect comment"
    interaction {
      foo = 2
      bar = 5
      true
    }
    println "foo"
    foo * bar == 10
    foo + bar == 7

    and:_ "My and comment"
    true
  }
}

控制台日志:

My given comment
blah
My expect comment
foo
My and comment

这篇关于当规范运行时,如何打印出spock元数据(给定的内容,何时|然后)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 18:30