Chapter 6. Parameterized Specifications
In “Block Taxonomy”, we briefly mentioned the where:
block with the promise that it would be explained more fully in a later chapter.
This is that chapter, and we will dive deep into the primary use of the where:
block: parameterized testing.
Parameterized testing is executing common test logic against different data.
“Data” typically means inputs or expected outputs.
In Spock, the common test logic is represented as a feature method, like those we’ve seen already, and the data is represented by parameters defined in a where:
block.
For each set of parameters, the feature method is executed once.
Thus, you can apply the same test to a variety of values, boundary conditions, or edge cases.
JUnit has a couple of mechanisms for doing parameterized testing, which we’ll look at briefly and hopefully see that Spock’s where:
block offers a much simpler and more flexible way to achieve the same result.
To begin, we’ll look at a simple feature method that checks a range of invalid values and ensures that they are all rejected.
We included some simple validation in the User
class we created in Chapter 2, but if we’re going to allow people to register for Squawker accounts, we should really ensure that their registration details are correct.
We’ll add a new RegistrationService
class that handles the creation of new user accounts.
For the moment, the only thing we need in order to create a User
is a username string. We can worry about passwords and ...
Get Spock: Up and Running now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.