The AAA Pattern: Arrange, Act, Assert
What You'll Learn
You'll learn how to structure unit tests using three clear phases that make tests easy to read, write, and maintain.
The Three-Phase Test Structure
The AAA pattern organizes every unit test into three distinct sections:
1. Arrange
Set up everything your test needs: create objects, prepare test data, configure dependencies. Think of this as setting the stage before the actors perform.
2. Act
Execute the specific code you're testing—usually a single method call or operation. This is the "main event" of your test.
3. Assert
Verify that the outcome matches your expectations. Check return values, object states, or that certain methods were called.
Why This Pattern Matters
Without structure, tests become confusing tangles of setup, execution, and verification mixed together. The AAA pattern acts like paragraphs in an essay—each section has a clear purpose, making the test's intention obvious at a glance.
When you (or a teammate) return to a test months later, you can immediately understand: "What scenario am I testing? What action triggers it? What should happen?"
Example Structure (Conceptual)
// Arrange: Create a calculator and test numbers
// Act: Add the numbers together
// Assert: Check that the sum equals the expected result
Many developers add blank lines or comments between sections to make the phases visually distinct, especially when the arrange section is lengthy.
Consistency Creates Clarity
Using AAA consistently across your test suite means anyone can jump into any test and quickly grasp what's happening. It reduces cognitive load and makes tests serve as clear documentation of how your code should behave.
Key Takeaway: Structure every unit test with Arrange (set up), Act (execute), and Assert (verify) to create clear, maintainable tests that document expected behavior.