5.2. Conditional logic

We write automated tests for multiple reasons. We look for safety from regression, expecting our tests to raise the flag when we stumble. We lean on tests to help us formulate the behavior that we want from our code. We also lean on tests to understand what the code does or what it should do. Particularly related to this last point, the presence of conditional logic in our tests is generally a bad thing. It’s a smell.

Let’s say we’re in the middle of a refactoring and running the tests to make sure everything still works, when we find that one of the tests is now failing. This comes as a surprise, we didn’t expect our change to affect that test, but that’s what happened. We check the stack trace from the test failure and ...

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.