Couple Inputs to Outputs

How many times have you seen tests that resemble the following?

assertEquals(4, transformIt(2));

What are the meanings of the literal values 2 and 4 in this code? Sure, you can read the documentation, assuming it exists, for transformIt() and know what it is supposed to do. In the absence of documentation, you can look at the implementation and reverse engineer the code, but then you are testing implementation, not intent. Also, the author of code such as this requires each following developer to decipher his own intent in addition to whatever intent needs to be extracted from the code under test.

Would you rather see something like Listing 5-1 instead?

Listing 5-1: Coupling inputs to outputs in tests

Get Quality Code: Software Testing Principles, Practices, and Patterns 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.