Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
leonard84 committed Nov 10, 2021
1 parent 06a851e commit 632fbe9
Showing 1 changed file with 41 additions and 14 deletions.
55 changes: 41 additions & 14 deletions docs/data_driven_testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ such a method.

== Unrolled Iteration Names

By default the names of unrolled iterations are the name of the feature, plus the data variables and the iteration
By default, the names of unrolled iterations are the name of the feature, plus the data variables and the iteration
index. This will always produce unique names and should enable you to identify easily the failing data variable
combination.

Expand Down Expand Up @@ -470,28 +470,54 @@ include::{sourcedir}/datadriven/DataSpec.groovy[tag=unrolled-3b]

Additionally, to the data variables the tokens `#featureName` and `#iterationIndex` are supported.
The former does not make much sense inside an actual feature name, but there are two other places
where an unroll pattern can be defined, where it is more useful.
where an unroll-pattern can be defined, where it is more useful.

[source,groovy,indent=0]
----
def "#person is #person.age years old [#iterationIndex]"() {
----

Alternatively to specifying the unroll pattern as method name, it can be given as parameter
will be reported as

----
└─ Spock ✔
└─ PersonSpec ✔
└─ #person.name is #person.age years old [#iterationIndex] ✔
├─ Fred is 38 years old [0] ✔
├─ Wilma is 36 years old [1] ✔
└─ Pebbles is 5 years old [2] ✔
----

Alternatively, to specifying the unroll-pattern as method name, it can be given as parameter
to the `@Unroll` annotation which takes precedence over the method name:

[source,groovy,indent=0]
----
@Unroll("#featureName[#iterationIndex] (#person.name is #person.age years old)")
def "person age should be calculated properly"() {
// ...
----

will be reported as

----
└─ Spock ✔
└─ PersonSpec ✔
└─ person age should be calculated properly ✔
├─ person age should be calculated properly[0] (Fred is 38 years old) ✔
├─ person age should be calculated properly[1] (Wilma is 36 years old) ✔
└─ person age should be calculated properly[2] (Pebbles is 5 years old) ✔
----

The advantage is, that you can have a descriptive method name for the whole test, while having a separate template for each iteration.
The advantage is, that you can have a descriptive method name for the whole feature, while having a separate template for each iteration.
Furthermore, the feature method name is not filled with placeholders and thus better readable.

If neither a parameter to the annotation is given, nor the method name contains a `#`,
the <<extensions.adoc#spock-configuration-file,configuration file>> setting `defaultPattern`
in the `unroll` section is inspected. If it is set to a non-`null`
string, this value is used as unroll pattern. This could for example be set to
string, this value is used as unroll-pattern. This could for example be set to

- `#featureName` to have all iterations reported with the same name, or
- `#featureName[#iterationIndex]` to have a simply indexed iteration name, or
Expand All @@ -509,30 +535,30 @@ This is the complete list of special tokens:

=== Configuration

.Set Default Unroll Pattern
.Set Default Unroll-Pattern
[source,groovy]
----
unroll {
defaultPattern '#featureName[#iterationIndex]'
}
----

If none of the three described ways is used to set a custom unroll pattern, by default
If none of the three described ways is used to set a custom unroll-pattern, by default
the feature name is used, suffixed with all data variable names and their values and
finally the iteration index, so the result will be for example
`my feature [x: 1, y: 2, z: 3, #0]`.

If there is an error in an unroll expression, for example typo in variable name, exception during
evaluation of a property or method in the expression and so on, the test will fail. This is not
true for the automatic fall back rendering of the data variables if there is no unroll pattern
true for the automatic fall back rendering of the data variables if there is no unroll-pattern
set in any way, this will never fail the test, no matter what happens.

The failing of test with errors in the unroll expression can be disabled by setting the
<<extensions.adoc#spock-configuration-file,configuration file>> setting `validateExpressions`
in the `unroll` section to `false`. If this is done and an error happens, the erroneous expression
`#foo.bar` will be substituted by `#Error:foo.bar`.

.Disable Unroll Pattern Expression Asserting
.Disable Unroll-pattern Expression Asserting
[source,groovy]
----
unroll {
Expand All @@ -556,20 +582,21 @@ With `includeFeatureNameForIterations true`
└─ Spock ✔
└─ ASpec ✔
└─ foo
├─ foo [x: 1, y: a, #0] ✔
├─ foo [x: 2, y: b, #1] ✔
└─ foo [x: 3, y: c, #2] ✔
└─ really long and informative test name that doesn't have to be repeated
├─ really long and informative test name that doesn't have to be repeated [x: 1, y: a, #0] ✔
├─ really long and informative test name that doesn't have to be repeated [x: 2, y: b, #1] ✔
└─ really long and informative test name that doesn't have to be repeated [x: 3, y: c, #2] ✔
----

.With `includeFeatureNameForIterations false`
----
└─ Spock ✔
└─ ASpec ✔
└─ foo
└─ really long and informative test name that doesn't have to be repeated
├─ x: 1, y: a, #0 ✔
├─ x: 2, y: b, #1 ✔
└─ x: 3, y: c, #2 ✔
----

NOTE: The same can be achieved for individual features by using `@Unroll('#dataVariablesWithIndex')`.

0 comments on commit 632fbe9

Please sign in to comment.