Structure and Naming

Three parts, in order. Arrange the situation, act on it, assert what should be true. Keeping them visually separate makes a test readable at a glance, and a test nobody can read is a test nobody will maintain.

Name it after the behavior. test_draft_is_not_visible_to_strangers tells you what broke from the failure output alone. test_view_2 tells you to go and read it.

A good name is a sentence about the system. When a test fails at 5pm, the name is the first and sometimes only thing anybody reads.

One reason to fail. A test asserting six unrelated things fails on the first and hides the other five, and when it fails you do not know which behavior broke.

Several assertions about one behavior are fine: checking a response's status and its content is one behavior. Checking a response, then a database row, then an email, is three tests wearing one name.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Assertions That Say Something →
← Back