Moving existing PHPUnit tests to PEST in Laravel application

Moving existing PHPUnit tests to PEST in Laravel application


As developer, we all know how crucial it is to write tests for your software. I like that feeling when your tests saved you making some bug in the application.

As we know, each language has different tests frameworks to write tests & these frameworks are evolving every day.

So far the in the PHP universe, PHPunit is the obvious choice to write your tests. PHPunit is the best and does the job very well.

Our developer friend Nuno Maduro take PHPunit to the next level & created new testing framework called PEST with simple goal in mind to provide more elegant way of writing test in PHP.

In the recent LaraconEU online, I saw a talk about PEST & how easy it is to start with it. In my case I've have an existing app build with Laravel & has tests written already using PHPUnit. So I decided to move tests in my app to PEST, to my surprise it was very easy job.

As mentioned on their website make sure to have right version of phpunit phpunit/phpunit:"^9.0" & collision(nunomaduro/collision:"^5.0").

After that, install PEST using composer composer require pestphp/pest --dev

Now you have PEST dependency installed, you just have to inject PEST into your existing laravel testsuite. Run artisan command php artisan pest:install

That's it, we ready to go. Now your existing PHPUnit tests should work as is with command ./vendor/bin/pest . Now you create alias pest & we are done.

Now my sample PEST test looks something like below. Now my test files has almost no boilerplate code to carry all the time, rather only the code that matters.

Output of my PEST test looks something like below. I like how readable the output is, also I can see coverage on the terminal which is cherry on top.

From my personal experience, moving all your existing Laravel test was not a big deal at all. If you are looking to improve tests on your application, I'll highly recommend going with pestphp.