Published February 10, 2020
by Doug Klugh

Guarding Test Conditions

Avoid conditional test logic by replacing if statements with assertions that fail when the conditions are not satisfied, preventing execution of statements that would cause test errors.  This will promote Defect Localization, document pre-conditions enforced by the guard assertions, and minimize the effort required to verify your tests.  If you have to write tests for your tests, when would it ever stop?

Don't expect to use guard assertions within unit tests, since they verify only one test condition.  However, guard assertions are often used within component, integration, and system tests.