Alex Luya
Alex Luya

Reputation: 9922

How to print out spock metadata(the conent of given|when|then) when specs running?

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

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"
...

I want that blow content got printed:

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

Upvotes: 1

Views: 3601

Answers (1)

kriegaex
kriegaex

Reputation: 67387

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):

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.


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

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
  }
}

Console log:

My given comment
blah
My expect comment
foo
My and comment

Upvotes: 2

Related Questions