tools

Enjoyable PHP Testing with PestPHP

December 23, 2022

//

1 min read

Writing tests is time-consuming and tedious. But if you want close your eyes without fear at night, having good tests in your applications is essential.

With Pest, testing is much more enjoyable. It has a simpler syntax compared to PHPUnit. The test reports have more useful information in a beautiful style.

Pest is a progressive testing framework that uses PHPUnit at its core. That means you can both PHPUnit test classes and Pest test files in the same test suite.

You can also convert your PHPUnit test classes to Pest test files using these tools: Pest Convertor by Laravel-Shift (Each Shift Costs 9$) Pest Convertor by mandisma (Open-Source and free!)

Laravel Shift is a well-known Service, especially among Laravel Users. It can convert your PHPUnit tests for 9$.

The open-source option is not that popular but I've used it on a medium-sized project with hundreds of tests, and it's worked very well for me but it may need more work in big projects with complicated test suites.

At the end let's compare a test written in both PHPUnit and Pest:

PHPUnit:

1<?php
2 
3 namespace Tests\Feature;
4 
5 use Tests\TestCase;
6 
7 class HomepageTest extends TestCase
8 {
9 /** @test */
10 public function it_renders_the_homepage()
11 {
12 $response = $this->get('/');
13 
14 $response->assertStatus(200);
15 }
16 
17 }

Pest:

1<?php
2 
3 it('renders the homepage', function () {
4 $response = $this->get('/');
5 
6 $response->assertStatus(200);
7 });

Beautiful version of this test using Pest:

1<?php
2 
3 it('renders the homepage')->get('/')->assertStatus(200);

Isn't the last example beautiful? We only wrote 1 line of code instead of 18 lines.

And that's why I love Pest that much. it's simple, minimal, and enjoyable to use.