Go for parameterized tests when tests have the same skeleton

A little duplication is never a problem, but a lot of duplication is. We created 21 different tests for the substringsBetween program. The test code was lean because we grouped some of the test cases into single test methods. Imagine writing 21 almost-identical test cases. If each method took 5 lines of code, we would have a test class with 21 methods and 105 lines. This is much longer than the test suite with the parameterized test that we wrote.

Some developers argue that parameterized tests are confusing. Deciding whether to use JUnit test cases or parameterized tests is, most of all, a matter of taste. I use parameterized tests when the amount of duplication in my test suite is too large. I leaned more toward JUnit test cases: lots of test cases logically grouped in a small set of test methods. We discuss test code quality further.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *