4.8. Setup sermon

In section 4.4 you learned about incidental details and how tests should reveal their intent, purpose, and meaning promptly and squarely. Most often we tackle such a problem by extracting the irrelevant details behind helper methods, leaving only the essential bits—the beef—in the test method to make it concise and to the point. Sometimes we move a bunch of code that prepares the scenario for a test into a setup method.

Just as we’d be foolish to pretend that test code doesn’t need the same degree of professional treatment as production code, we’d be mistaken to think that messy code would be any less dire a problem when it’s found in a setup method.

Let’s look at an example that illustrates this problem in concrete terms.

Get Effective Unit Testing 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.