Courses

Testing in Laravel 11: Advanced Level

Pest 3: Mutation Testing

Summary of this lesson:
- Understand mutation testing concept
- Run mutation tests with Pest
- Identify weaknesses in test suites
- Improve test coverage
- Ignore specific mutations
- Analyze and enhance test quality

This is a new feature in the Pest 3 version.

Mutation testing makes small changes (mutations) to your code and then runs the tests to check if they are still passing. It's a great way to identify weaknesses in your test suite.

Notice: this feature requires XDebug 3.0+ or PCOV.


Running Mutation Test

To start mutation testing, first, you must specify which part you are testing using the covers() method in the test.

For example, Laravel Breeze comes with some Pest tests. Let's check the registration testing.

tests/Feature/Auth/RegistrationTest.php:

covers(\App\Http\Controllers\Auth\RegisteredUserController::class);
 
test('registration screen can be rendered', function () {
$response = $this->get('/register');
 
$response->assertStatus(200);
});
 
test('new users can register', function () {
$response = $this->post('/register', [
'name' => 'Test User',
'email' => '[email protected]',
'password' => 'password',
'password_confirmation' => 'password',
]);
 
$this->assertAuthenticated();
$response->assertRedirect(route('dashboard', absolute: false));
});

Now, we can run the test suite with mutation testing.

php artisan test --mutate

We can see that the mutation shows 18 mutations untested, and the score is 28%.


Adding One Mutation

One of the untested cases is about the Event. And if we look...

The full lesson is only for Premium Members.
Want to access all 31 lessons of this course? (74 min read)

You also get:

  • 69 courses (majority in latest Laravel 11)
  • Premium tutorials
  • Access to repositories
  • Private Discord