What is Pest and Why You Should Use It

Moumen Alisawe
3 min readApr 7, 2023

--

Introduction:

PHP testing is an essential part of any web development project. It helps to ensure the quality, reliability, and security of the code. However, many PHP developers find testing to be tedious, complex, and boring. That’s why Pest, a new testing framework for PHP, aims to change that.

Pest is an elegant PHP testing framework that extends PHPUnit, the most popular testing tool for PHP. Pest offers a simple and expressive syntax that makes writing tests easy and enjoyable. Pest also provides a beautiful and interactive console output that makes debugging fast and fun.

Features:

Some of the features and benefits of Pest are:

  • Pest uses a one-line code syntax that reduces the boilerplate code of PHPUnit. For example, instead of writing:
<?php

use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
public function test_example()
{
$this->assertTrue(true);
}
}

You can write:

<?php

it('example', function () {
assertTrue(true);
});
  • Pest improves the output of PHPUnit by using Collision, a package that provides colorful and readable errors and stack traces. Pest also shows the progress of the tests, the execution time, and the code coverage in a clear and concise way.
  • Pest adds scalability with datasets, a feature that allows you to run the same test with different sets of data. For example, you can write:
<?php

it('sums two numbers', function ($a, $b) {
expect($a + $b)->toBe(4);
})->with([
[1, 3],
[2, 2],
]);

This will run the test twice, with different values for $a and $b.

Pest supports plugins that extend its functionality and integrate with other frameworks and tools. For example, there is a plugin for Laravel that allows you to use Laravel’s testing features with Pest. There are also plugins for Livewire, Parallel Testing, Expectations, Snapshots, Faker, Mockery, and more.

For example, with the Laravel plugin, you can write tests like this:

<?php
use App\Models\User;

test('a user can be created', function () {
$user = User::factory()->create();
$this->assertDatabaseHas('users', [
'email' => $user->email,
]);
});

Conclusion:

Pest is a testing framework built for humans. It makes testing simple, minimal, and elegant. It is powered by PHPUnit, so it is compatible with any existing PHPUnit test suite. It is also open source and has a growing community of contributors and users.

If you want to learn more about Pest or try it out yourself, you can visit its website at pestphp.com or check out its GitHub repository at github.com/pestphp/pest. You can also follow its Twitter account at @pestphp or join its Discord or Telegram channels.

Pest is the future of PHP testing because it brings back the joy of testing to PHP developers. With Pest, testing can be more productive, more enjoyable, and more elegant.

--

--

Moumen Alisawe

Software engineer with 6+ years developing mobile and web apps. Passionate about creating seamless user experiences. Let's build something great together.