Spock: data-driven features
A where: block turns one feature into a table: each row is one run, and the report names the row that failed. It is the reason many teams write their tests in Spock rather than in a loop of assertions.
The class under test
Slug.groovy
class Slug {
static String of(String text) {
text.trim().toLowerCase().replaceAll(/\s+/, '-')
}
}
A table of rows
SlugSpec.groovy
import spock.lang.Specification
import spock.lang.Unroll
class SlugSpec extends Specification {
@Unroll
def 'turns "#title" into "#slug"'() {
expect:
Slug.of(title) == slug
where:
title | slug
'Groovy Course' | 'groovy-course'
' Hello World ' | 'hello-world'
'Spock' | 'spock'
}
}
The table is a set of variables and one row per run, and @Unroll makes each row a test of its own in the report, with the values written into the name. Without it the report shows one feature and a failure somewhere inside the table.
Run it
CP=/opt/spock/spock-core-2.4-groovy-5.0.jar:/opt/spock/byte-buddy-1.17.5.jar:/opt/spock/geantyref-1.3.16.jar:/opt/spock/hamcrest-3.0.jar:/opt/spock/objenesis-3.4.jar:/opt/groovy/lib/groovy-5.0.8.jar
groovyc -cp "$CP" -d out Slug.groovy SlugSpec.groovy
junit execute --class-path "out:$CP" --select-class SlugSpec --disable-ansi-colors
Thanks for using JUnit! Support its development at https://junit.org/sponsoring
╷
├─ JUnit Platform Suite ✔
├─ JUnit Jupiter ✔
├─ JUnit Vintage ✔
└─ Spock ✔
└─ SlugSpec ✔
└─ turns "#title" into "#slug" ✔
├─ turns "Groovy Course" into "groovy-course" ✔
├─ turns " Hello World " into "hello-world" ✔
└─ turns "Spock" into "spock" ✔
Test run finished after 2150 ms
[ 6 containers found ]
[ 0 containers skipped ]
[ 6 containers started ]
[ 0 containers aborted ]
[ 6 containers successful ]
[ 0 containers failed ]
[ 4 tests found ]
[ 0 tests skipped ]
[ 4 tests started ]
[ 0 tests aborted ]
[ 4 tests successful ]
[ 0 tests failed ]
Three rows, three tests, three ticks. A row that fails names itself in the tree, which is the next example.
A row that fails
The spec below has one row and a wrong expectation. The report is the real one: Spock prints the condition, the value it got and the value it expected, and the launcher exits with status 1.
BrokenSlugSpec.groovy
import spock.lang.Specification
class BrokenSlugSpec extends Specification {
def 'a row that does not match'() {
expect:
Slug.of('Groovy Course') == 'groovy_course'
}
}
CP=/opt/spock/spock-core-2.4-groovy-5.0.jar:/opt/spock/byte-buddy-1.17.5.jar:/opt/spock/geantyref-1.3.16.jar:/opt/spock/hamcrest-3.0.jar:/opt/spock/objenesis-3.4.jar:/opt/groovy/lib/groovy-5.0.8.jar
groovyc -cp "$CP" -d out Slug.groovy BrokenSlugSpec.groovy
junit execute --class-path "out:$CP" --select-class BrokenSlugSpec --disable-ansi-colors
Thanks for using JUnit! Support its development at https://junit.org/sponsoring
╷
├─ JUnit Platform Suite ✔
├─ JUnit Jupiter ✔
├─ JUnit Vintage ✔
└─ Spock ✔
└─ BrokenSlugSpec ✔
└─ a row that does not match ✘ Condition not satisfied:
Slug.of('Groovy Course') == 'groovy_course'
| | |
| groovy-course false
class Slug 1 difference (92% similarity)
groovy(-)course
groovy(_)course
Failures (1):
Spock:BrokenSlugSpec:a row that does not match
MethodSource [className = 'BrokenSlugSpec', methodName = 'a row that does not match', methodParameterTypes = '']
=> Condition not satisfied:
Slug.of('Groovy Course') == 'groovy_course'
| | |
| groovy-course false
class Slug 1 difference (92% similarity)
groovy(-)course
groovy(_)course
BrokenSlugSpec.a row that does not match(BrokenSlugSpec.groovy:6)
Test run finished after 2394 ms
[ 5 containers found ]
[ 0 containers skipped ]
[ 5 containers started ]
[ 0 containers aborted ]
[ 5 containers successful ]
[ 0 containers failed ]
[ 1 tests found ]
[ 0 tests skipped ]
[ 1 tests started ]
[ 0 tests aborted ]
[ 0 tests successful ]
[ 1 tests failed ]
The line the page is about is Condition not satisfied: the left side produced groovy-course and the expectation names groovy_course. A typo in the expectation is the most common failure of a table, and the report shows both values instead of a line number, which is the whole argument for the format.
Related
- Spock: feature methods: the blocks a table fills.
- Spock: mocks and stubs: the other labels of a feature.
Автор статьи: Arthur Isaev