Chapter 5. Parameterized tests

 

This chapter covers

  • Definition of parameterized tests
  • Using the where: block
  • Understanding data tables and data pipes
  • Writing custom data providers

The previous chapter presented all the Spock blocks that you can use in a unit test, except for one. I left out the where: block on purpose because it deserves a chapter of its own. The where: block is used for parameterized tests. Parameterized tests are unit tests that share the same test logic (for example, when the temperature goes up, the reactor must shut down), but need to run on different parameters (for example, with low temperature and then with high temperature) in order to account for all cases.

This chapter covers both some theory on when to use parameterized tests and facilities Spock offers for parameterized testing. You might already have seen parameterized tests with JUnit, so feel free to skip the first section and start reading at section 5.2 for the specific Spock features if you’re already familiar with the concept of parameterized testing.

5.1. Detecting the need for parameterized tests

5.2. Using the where: block

5.3. Using data pipes for calculating input/output parameters

5.4. Using dedicated data generators

5.5. Working with third-party data generators

5.6. Summary