5.1. Detecting the need for parameterized tests

Experienced developers usually can understand the need for a parameterized test right away. But even if you’re just starting with unit tests, an easy rule of thumb can show you the need for a parameterized test. Every time you start a new unit test by copying-pasting an existing one, ask yourself, “Is this test that much different from the previous one?” If you find yourself duplicating unit tests and then changing only one or two variables to create a similar scenario, take a step back and think about whether a parameterized test would be more useful. Parameterized tests will help you keep the test code DRY.[1]

1

This acronym stands for don’t repeat yourself. See https://en.wikipedia.org/wiki/Don’t_repeat_yourself ...

Get Java Testing with Spock 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.