When writing Unit or Feature tests in Laravel, one of the most effective ways to keep your tests clean and easy to follow is by using the AAA pattern.
What is AAA?
AAA stands for:
- Arrange : Set up your data and dependencies
- Act : Execute the behavior or function you want to test
- Assert : Verify that the result matches your expectations
This structure makes your tests:
- Easier to read
- Easier to maintain
- Easier to debug
Why Use AAA?
It helps you avoid writing "messy" tests where setup, execution, and assertions are all mixed together.
By clearly separating the 3 phases of testing, you're more likely to catch bugs early, understand what each test is doing at a glance, and maintain consistency across your codebase.
Laravel Example
Here’s a simple Feature Test
example following the AAA pattern:
public function test_run_todo_show(): void
{
// Arrange
$todo = Todo::factory()->create();
// Act
$response = $this->get("/todos/{$todo->id}");
// Assert
$response->assertStatus(200);
$response->assertViewIs('todos.show');
$response->assertSee($todo->title);
}
Summary
The AAA pattern stands for Arrange, Act, Assert.
It separates test setup, execution, and verification for clarity and maintainability.
This structure improves readability, consistency, and debugging.
Top comments (0)