Pest: a PHP testing framework that goes above and beyond PHPUnit

Peter Villani - Feb 18 '22 - - Dev Community

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, but we switched over to the Pest Testing Framework, 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.

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, 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:

it('is an example, function () {
    assertTrue(true);
});
Enter fullscreen mode Exit fullscreen mode

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:

it('is an example', function ()->assertTrue(true);
Enter fullscreen mode Exit fullscreen mode

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

namespace Tests\Unit;
use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
    /** @test */
    public function testBasicTest()
    {
        $this->assertTrue(true);        
    } 
    // ...
}
Enter fullscreen mode Exit fullscreen mode

Improving the output

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

phpunit testing output

While less verbosity is generally good, we felt we needed a bit more. Pest gave us more information per test:

pest testing output

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

pest testing output failure

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

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):

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

You can use multi-dimensional arrays as well:

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

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 
Enter fullscreen mode Exit fullscreen mode

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:

dataset('emails', function () {
   Return (['other@example.com', 'enunomaduro@gmail.com'];
]);
Enter fullscreen mode Exit fullscreen mode

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

it('has emails', function ($email) {
   expect($email)->not->toBeEmpty();
   assertTrue(true);
})->with('emails');
Enter fullscreen mode Exit fullscreen mode

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:

it('has home', function () {
   // ..
})->skip();
Enter fullscreen mode Exit fullscreen mode

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, built by Nuno Maduro from @laravel.

. . . . . . . . . . . . . . . . . . . . . . .