Logging block labels in real-time

Skip to first unread message

Brian Stewart

May 20, 2016, 3:55:56 PM5/20/16
to Spock Framework - User
Hi, all! Is it possible to turn on real-time logging (not post-run reports) that display the block labels? Here is an example of what I'm trying to do:

def 'test block-level logging'() {
: 'some setup'
def x = 5
when: 'action is taken'

then: 'result matches expected'
== 6

I would like this spec to produce logging output something like this (with or without the "given"/"when"/"then"):
given: some setup
when: action is taken
then: result matches expected

I looked at some extensions, but only saw hooks for before-spec and before-feature, not before-block.

Any pointers would be appreciated!

David Lumpkin

May 24, 2016, 1:49:24 PM5/24/16
to Spock Framework - User
I've looked for this too but never found a good way to do it

Raghu Kiran

Sep 15, 2016, 11:38:12 AM9/15/16
to Spock Framework - User
BDD test case writing is cool, This shoud reflect in reporting too. 

AbstractRunListener does not have beforeBlock and afterBlock contracts in Interface... 

Is there a way we can raise an enhancement request for this to be included to spock? Logging is where the teams and folks look at after automation runs and having good log that says what failed at which Block certainly helps. 

Brian Stewart

Mar 3, 2017, 4:05:25 PM3/3/17
to Spock Framework - User
I just stumbled across a simple way to do this, and thought I'd update here. Just add the logging statement where the block description would be. A wrapper function around `log.info()` would make it even cleaner. 

def 'test block-level logging'() {

: log.info 'some setup'
def x = 5



Robert Evans

Mar 7, 2017, 12:30:28 PM3/7/17
to Spock Framework - User
You can make the method name a more dynamic which turns the problem on its head a little but ultimately gives you what you want which is presumably to see the values of your variables and make your programs easier to debug.

def "when I add #addValue to x=#x the result is #expected"() {
def result = x + addValue

then: // (possibly expect: I cannot remember.)
assert result == expected

| x | addValue | expected  |
| 5 | 1        | 6         |

For more than 1 row in the where: portion use the @Unroll annotation above the method name to let spock know to treat each row of input as a single independent test - otherwise it'll assume your assertions are all a part of a single larger test and you won't get the right output.

Reply all
Reply to author
0 new messages