Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. The test 'def' should describe a high level scenario (not set up details or expected outcome)

  2. Each label should have a description. The label plus description should form a readable English sentence preferring high level (Product Owner)
    type language over code level langue i.e. no method names etc.
    (since the label is the first word of the sentence the description should start with a lower case character and end with a full stop)

  3. All blocks should be separated by a blank line except and 'and: ' block which really is a continuation of the previous block

  4. Given-when-then v. expect. Given-when-then is the preferred format but expect block is useful in situations where it 'is more natural to describe
    stimulus and expected response in a single expression' see also the Spock Primer

    Code Block
    languagegroovy
    titleExpect and Labels Example
    @Unroll
    def 'Celltrace scanner activation request with invalid ropPeriod value of #ropPeriod.'() {
    	given: 'scanner details with an invalid rop period'
    		eventHeaders = eventHeadersForCellTrace(ropPeriod, 'STREAMING')
    
    	expect: 'the outbound request XML contains the default rop period of "FIFTEEN_MIN"'
    		activateEventJobRequest.getRequest(eventHeaders).contains("<reportingPeriod>FIFTEEN_MIN</reportingPeriod>")
    
    	where: 'rop period is any of these invalid values'
    		ropPeriod << [ null, 0, -1, 123 ]
    }


  5. Expected outputs in Spock data tables should be separated with a double pipe symbol (||) to visually set them apart


Use of assert keyword

The assert keyword is optional in 'expect:'  or 'then:' blocks. Groovy will automatically 'assert' any Boolean expression in these blocks.
Depending on the complexity of the expression and the amount of code in this block it might help to highlight what is being asserted using the 'assert' keyword. It is also a mater of personal choice this convention does not enforce either approach. 

In the example above we could have used

Code Block
languagegroovy
titleExpect and Labels Example
	expect: 'the outbound request XML contains the default rop period of "FIFTEEN_MIN"'
		assert activateEventJobRequest.getRequest(eventHeaders).contains("<reportingPeriod>FIFTEEN_MIN</reportingPeriod>")

Note. 'Assert' is required in method or closures that arre called from these block if those methods contain Boolean expressions that need to be asserted!


Some more tips from the Spock Primer

...