Agent Wranglers

TST-8Recommended

Give each test one reason to fail

Each test checks one behaviour. When it fails, its name tells you what broke.

How to do it

Split a test that checks unrelated things into several tests. Several assertions are fine when they describe one result, such as the fields of one returned object. Name tests as plain sentences, such as "rejects an expired coupon". Reviewers check those names as Review tests like code says.

Examples

PHP example

// Bad: two behaviours in one test
public function testCoupons(): void { /* expired and wrong-currency cases */ }

// Good: one behaviour each
public function testRejectsAnExpiredCoupon(): void { /* ... */ }
public function testRejectsACouponInAnotherCurrency(): void { /* ... */ }

Python example

# Good: one behaviour each
def test_rejects_an_expired_coupon(): ...

def test_rejects_a_coupon_in_another_currency(): ...

How to check

Break one behaviour on purpose. Exactly one test, or a small group about that behaviour, fails.

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/testing/unit-tests/one-reason-to-fail">Agent Wranglers testing playbook</a>. In code you write, add a comment next to it with the same link.