Arrange-Act-Assert Pattern
You'll learn an alternative way to structure unit tests that makes the three phases of testing crystal clear through explicit separation.
What Is Arrange-Act-Assert?
The Arrange-Act-Assert (AAA) pattern is a testing structure that organizes your test into three distinct sections, each with a specific purpose. This pattern is essentially the same concept as Given-When-Then, but uses different terminology that many developers find more natural when writing code-focused unit tests.
The Three Phases
Arrange: Set up everything your test needs—create objects, configure dependencies, prepare test data. This is your setup phase.
Act: Execute the specific behavior you're testing—call the method, trigger the action. This should typically be a single line.
Assert: Verify the outcome matches your expectations—check return values, state changes, or side effects.
Why Use This Pattern?
The AAA pattern provides a mental checklist that ensures every test is complete and well-structured. The explicit naming helps both the test writer and future readers quickly understand what's happening. Many developers find "Arrange-Act-Assert" more intuitive than "Given-When-Then" when writing technical unit tests, though they accomplish the same goal.
Think of it like a science experiment: you arrange your lab equipment and materials, you act by conducting the experiment, and you assert whether the results match your hypothesis.
Readability Tip
Some teams add blank lines or comments between the three sections to make the structure visually obvious, especially in longer tests.
Key Takeaway: The Arrange-Act-Assert pattern structures tests into three clear phases—setup, execution, and verification—making tests easier to write, read, and maintain.