Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spock: test name and outcome in setup() cleanup() methods

I am wondering if it is possible to make the following data available in a Spock test's lifecycle methods:

  • test name in the setup() method
  • test name and test outcome in the cleanup() method

The reason I am asking is that I would like send these metadata to a data sink without the need to touch each and every test.

Thanks! Martin

like image 614
metmajer Avatar asked Feb 13 '23 10:02

metmajer


2 Answers

Try this.specificationContext.iterationInfo.name in setup(). Not sure if it works. In general specificationContext is the place where I'd be looking for such data.

like image 86
Opal Avatar answered Feb 14 '23 23:02

Opal


Update from Opal's answer:

It is now (Spock 1.1) specificationContext.currentIteration.name

For the record, you could also use

@Rule
TestName testName = new org.junit.rules.TestName()
...
println "name: $testName.methodName"

... but there seems no point.

For the other question, getting the outcome: I couldn't find a way of getting this from Spock's SpecificationContext.

I took a quick look at org.junit.rules.TestWatcher ... but with Spock this seemed unable to detect a failed test.

like image 34
mike rodent Avatar answered Feb 15 '23 00:02

mike rodent