From PHPUnit to Pest
Pest is a great testing framework built on top of PHPUnit. It has an elegant syntax simplifying your tests as much as possible.
Starting to work with Pest is very easy and we will see how to write more readable tests with it if you are used to PHPUnit.
Installation
First, install Pest via the Composer package manager:
composer require pestphp/pest --dev --with-all-dependencies
On Laravel, install the pest-plugin-laravel
and run the pest:install
Artisan command:
composer require pestphp/pest-plugin-laravel --dev
php artisan pest:install
Running your tests
You are probably used to execute vendor/bin/phpunit
to run your tests. With Pest, you have to run vendor/bin/pest
.
There is no difference in usage, you can continue to use the same options as with PHPUnit (--filter
for example).
You can have PHPUnit and Pest tests in the same project. The vendor/bin/pest
command will run your PHPUnit tests as well. But vendor/bin/phpunit
will not run your Pest tests.
Files structure
No change here. Pest uses the same file structure as PHPUnit.
tests
- Unit/ComponentTest.php
- Feature/HomeTest.php
phpunit.xml
Skeleton
With Pest, there is no class. You just have to use the helper test()
and a closure.
With PHPUnit, we do:
With Pest, we do:
For convenience, you can also use the it()
helper which is an alias of test()
so you can writeit('renders the homepage', ...)
.
Test Case
The closure you provide to your test function is always bound to a specific test case class. By default, that class is extending PHPUnit\Framework\TestCase
.
If you have to change it, you can call the uses()
function:
It will affect all tests of your file.
You may also use a specific test case class for all tests contained in a specified folder. For that, you can call the in()
function in your tests/Pest.php
file.
<?phpuses(Tests\TestCase::class)->in('Feature');// ...
Traits
uses()
is not only for test case classes. It’s the function you have to use to apply traits.
With PHPUnit, we do:
With Pest, we do:
Setup And Teardown
In your tests, you may want to run some code before and after each test or file.
With PHPUnit, we do:
With Pest, we do:
Data Providers / Datasets
Data Providers allows you to run the same test multiple times with different data. With Pest, it’s called Datasets.
With PHPUnit, we do:
With Pest, we do:
Pest offers more features than PHPUnit, like Shared Datasets or Lazy Datasets.
Expectations
With PHPUnit, you have assertions. You can use them with Pest as well, without any change. But in addition to that, Pest offers expectations. Expectations allow you to write your tests like you would a natural sentence.
There are lot of available expectations like for example:
As you can see, switching from PHPUnit to Pest is not difficult, and brings a lot of readability to your tests. Besides the simplicity, Pest offers new and useful features that I let you discover on the documentation.
I hope you enjoyed this post. Feel free to give it a clap! 👏
👋 I offer tech consulting services for companies that need help with their Laravel applications. I can assist with upgrades, refactoring, testing, new features, or building new apps. Feel free to contact me through LinkedIn, or you can find my email on my GitHub profile.