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.

Spock is flexible when it comes to parameterized tests. If offers a complete portfolio of techniques adaptable to your situation, no matter the complexity of your test data. The most basic format of parameterized tests (data tables) was already demonstrated in chapter 3. This chapter also explains data pipes (the underlying mechanism of data tables) and shows how to write custom data providers with Spock, which is the most flexible solution (but needs more programming effort).

All these Spock techniques have their own advantages and disadvantages with regards to readability and flexibility of unit tests, so it’s important to understand the trade-offs between them. Understanding when to use each one is one of the running themes of this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.103.210