Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Pest 3: Mutation Testing

Premium
3 min read

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 of our courses? (31 h 16 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

L
LogicKill ✓ Link copied!

Hi,

i have followed the steps in this tutorial, fresh laravel installation with laravel Breeze. Then i added the line

covers(\App\Http\Controllers\Auth\RegisteredUserController::class);

in tests/Feature/Auth/RegistrationTest.php.

But my results are the following everytime:

Tests: 2 passed (4 assertions) Duration: 138.34s

Mutating application files... 0 Mutations for 0 Files created Mutations: 0 tested Score: 0.00% Duration: 0.06s

INFO No mutations created.

What do i wrong?

Greets

LogicKill

M
Modestas ✓ Link copied!

Have you installed XDebug or PCOV? Without them, it should not work

L
LogicKill ✓ Link copied!

yes i have installed XDebug version 3.4.1

M
Modestas ✓ Link copied!

Can you check if it's enabled? Maybe it got installed, but it's still not enabled :)

You can do that in a few ways:

https://stackoverflow.com/a/22698209/21185594

L
LogicKill ✓ Link copied!

yes xdebug is enabled.

zend_extension = xdebug

and the following to:

xdebug.mode=coverage

The coverage from the tests with code-coverage is working with the reports in html.

M
Modestas ✓ Link copied!

I have tried this - and it seems to be working, so not sure what could be wrong here, sorry :(

L
LogicKill ✓ Link copied!

okay, thanks for your help. I will look if i can find die Problem.

L
LogicKill ✓ Link copied!

I have installed XAMPP with XDEBUG new, but the Problem is still there. Wenn i have not the line xdebug.mode=coverage in PHP.ini, the test failed.

The Mutation don't work at all.