Search code examples
testingspock

Does the "given" word serve any purpose?


As far as I can tell, the expression given: (starting the "given" block) does nothing at all in Spock tests and they function exactly the same if you just omit it.

Does anyone know different?


Solution

  • From the Spock documentation,

    The given block is where you do any setup work for the feature that you are describing. It may not be preceded by other blocks, and may not be repeated. A given block doesn’t have any special semantics. The given: label is optional and may be omitted, resulting in an implicit given block. Originally, the alias setup: was the preferred block name, but using given: often leads to a more readable feature method description (see Specifications as Documentation).

    So, ultimately yes, it functions just the same if you omit it, because Spock implicitly puts the label there anyway; but making the given label explicit can help to document your tests.