Introducing new developer-friendly pricing
Hey there, developers! At Algolia, we believe everyone should have the opportunity to bring a best-in-class search experience ...
VP of Product Growth
Hey there, developers! At Algolia, we believe everyone should have the opportunity to bring a best-in-class search experience ...
VP of Product Growth
Eye-catching mannequins. Bright, colorful signage. Soothing interior design. Exquisite product displays. In short, amazing store merchandising. For shoppers in ...
Search and Discovery writer
Ingesting data should be easy, but all too often, it can be anything but. Data can come in many different ...
Staff Product Manager, Data Connectivity
Everyday there are new messages in the market about what technology to buy, how to position your company against the ...
Chief Strategic Business Development Officer
Done any shopping on an ecommerce website lately? If so, you know a smooth online shopper experience is not optional ...
Sr. SEO Web Digital Marketing Manager
It’s hard to imagine having to think about Black Friday less than 4 months out from the previous one ...
Chief Strategic Business Development Officer
What happens if an online shopper arrives on your ecommerce site and: Your navigation provides no obvious or helpful direction ...
Search and Discovery writer
In part 1 of this blog-post series, we looked at app interface design obstacles in the mobile search experience ...
Sr. SEO Web Digital Marketing Manager
In part 1 of this series on mobile UX design, we talked about how designing a successful search user experience ...
Sr. SEO Web Digital Marketing Manager
Welcome to our three-part series on creating winning search UX design for your mobile app! This post identifies developer ...
Sr. SEO Web Digital Marketing Manager
National No Code Day falls on March 11th in the United States to encourage more people to build things online ...
Consulting powerhouse McKinsey is bullish on AI. Their forecasting estimates that AI could add around 16 percent to global GDP ...
Chief Revenue Officer at Algolia
How do you sell a product when your customers can’t assess it in person: pick it up, feel what ...
Search and Discovery writer
It is clear that for online businesses and especially for Marketplaces, content discovery can be especially challenging due to the ...
Chief Product Officer
This 2-part feature dives into the transformational journey made by digital merchandising to drive positive ecommerce experiences. Part 1 ...
Director of Product Marketing, Ecommerce
A social media user is shown snapshots of people he may know based on face-recognition technology and asked if ...
Search and Discovery writer
How’s your company’s organizational knowledge holding up? In other words, if an employee were to leave, would they ...
Search and Discovery writer
Recommendations can make or break an online shopping experience. In a world full of endless choices and infinite scrolling, recommendations ...
Feb 17th 2022 engineering
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 robust testing framework built on top of PHP’s standard testing library PHPUnit.
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); });
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);
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); } // ... }
PHPUnit offers PHP users the ability to visualize the testing results in a very compact read:
While less verbosity is generally good, we felt we needed a bit more. Pest gave us more information per test:
For errors, Pest gives you direct access to the line of code that has failed:
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:
As with most tests, the quality of the data is key. Along with using realistic and relevant mocked-up content, testing quality data requires:
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' ]);
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'] ]);
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:
dataset('emails', function () { Return (['other@example.com', 'enunomaduro@gmail.com']; ]);
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');
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();
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.
Powered by Algolia Recommend