Pest: a PHP testing framework that goes above and beyond PHPUnit
Tags: testing, php, phpunit, laravel
Published: 2022-02-18 08:56:54

When building third-party APIs, it’s important to test your code in every runtime scenario you’ve seen or can foresee. For this, a robust and easy to use & maintain testing framework is a must. As PHP developers, we relied heavily on [PHPUnit](https://phpunit.de/), but we switched over to the [Pest Testing Framework](https://pestphp.com/), which simplified and reduced our large library of testing code.

Given the number of tests we perform, and trying to cover every function in our codebase, we needed to simplify the testing code and make it easier to maintain, understand, and debug. We also wanted to make sure our testing framework would require little effort to integrate into [Laravel](https://laravel.com/). 

In this article, we take a look at Pest, a clean & robust testing framework built on top of PHP’s standard testing library PHPUnit.

## Reducing the testing codebase of PHPUnit with PHP chaining

Inspired in part by the one-line code “it” syntax in [Ruby Rspec](https://relishapp.com/rspec/rspec-core/docs/subject/one-liner-syntax), Pest’s own syntax is fairly straightforward. Pest removes the namespace and library references needed in PHPUnit, and it doesn’t require _extending_ hundreds of functions. It simply needs you to specify the test function – making it far easier to maintain and debug: 

```php
it('is an example, function () {
    assertTrue(true);
});
```

Still, 3 lines of code might be too much when multiplied by 100s of tests. Pest allows you to _chain_ the functions. The result is a one liner:

```php
it('is an example', function ()->assertTrue(true);
```

Thus, Pest has reduced the below 8 lines of PHPUnit code to the above 1 line of code: 

```php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
 
class ExampleTest extends TestCase
{
    /** @test */
    public function testBasicTest()
    {
        $this->assertTrue(true);        
    } 
    // ...
}
```

## Improving the output

PHPUnit offers PHP users the ability to visualize the testing results in a very compact read:

![phpunit testing output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mrd9pu0ptj85vhzhmqgd.png)

While less verbosity is generally good, we felt we needed a bit more. Pest gave us more information per test:
 
![pest testing output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/74kco8e38u6brswu7xaj.png)

For errors, Pest gives you direct access to the line of code that has failed:

![pest testing output failure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vp3wmj0dthzv7lc845lh.png)

Finally, you can get more info from the `coverage` option, if you want to display the number of lines of source code that have been tested and executed:

![pest testing output verbose](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svmapsudknue2ddkoiyb.png)

## Adding scalability with datasets

As with most tests, the quality of the data is key. Along with using realistic and relevant mocked-up content, testing quality data requires:

- Performing multiple tests on small and large variations of data
- Ability to add new use cases without any recoding

With Pest’s “datasets”, you can create one test that takes an inline array of data. So, for example, you can test multiple emails, each one representing a different use case (maybe connectivity):

```php
it('has emails', function ($email) {
   expect($email)->not->toBeEmpty();
})->with([
   'someone@jmail.com',
   'other@example.com'
]);
```

You can use multi-dimensional arrays as well:

```php
it('has emails', function ($name, $email) {
   expect($email)->not->toBeEmpty();
})->with([
   ['Someone', 'someone@jmail.com'],
   ['Other', 'other@example.com']
]);
```

That’s how it’s done inline. You can also move the data outside of the test to gain more flexibility, For this, you’ll need the following folder structure:

```
—Tests
—---testemails.php
—Datasets
—---emails.php 
```

Where the test functions go in folder `Tests`, and the datasets are defined in individual files in the `Datasets` folder. Here’s an example of the contents of the dataset file `emails.php`, which replaces the above inline email references:

```php
dataset('emails', function () {
   Return (['other@example.com', 'enunomaduro@gmail.com'];
]);
```

And so the test function can now reuse `emails.php` for testing:

```php
it('has emails', function ($email) {
   expect($email)->not->toBeEmpty();
   assertTrue(true);
})->with('emails');
```

## And more

There are many additional features and options, as well as a number of assertions, expectations, and exceptions. And don’t forget that Pest is an extension of PHPUnit, so you can perform actions before and after a test, like `setup` in PHPUnit.

Here’s a last example. You can `skip` tests:

```php
it('has home', function () {
   // ..
})->skip();
```

The `skip` function shows you how Pest really thinks about the tester and simplifies the important things. I’d advise you not to “skip” on Pest – it’s really a great addition to the PHP ecosystem.

Learn more about [Pest](https://pestphp.com/), built by [Nuno Maduro](https://twitter.com/enunomaduro) from [@laravel](https://github.com/laravel).