We often face a scenario where we have to run the same piece of test code over and over with different data. Spock’s data-driven testing is the answer for it. Let’s take a very common scenario of testing an action that creates the User record. This action takes username, firstName, lastName and other properties. Our testing code in Spock would look like:

That’s the bare-bone code for testing but how we are supposed to supply the data repetitively? Let’s add the Spock’s data-driven testing support to supply data in an iterative fashion. We do that with “where:” clause at the end of the test method like:

The data table you see in the end holds the variables and their values which would be supplied in the when: and then: clauses one-by-one. That’s it. You have used the Spock’s data-driven support. But there’s one glitch to it. If you look at the generated report of this test, you’ll find that only one test case is generated for this test case that has created 5 users. Seems unfair, isn’t it? Here comes “Unroll” feature to the rescue. If you annotate the test method with @Unroll.

Now, one test case will be created to report one iteration of the data. And the report would look like:

Is it only me or you also think it could have rocked if we could replace those weird numbers with something more readable? Well, say no more, you can add placeholder to show the values in the test-case’s title. You will have to edit the name to something like:

You can add placeholders for the test case variables by escaping them with #. And this would generate the report that would look like:

You can use more placeholder to make your test cases more readable in the report. That’s all to it. Hope, this helps you. 🙂