Search by Algolia
Introducing new developer-friendly pricing
algolia

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 ...

Nick Vlku

VP of Product Growth

What is online visual merchandising?
e-commerce

What is online visual merchandising?

Eye-catching mannequins. Bright, colorful signage. Soothing interior design. Exquisite product displays. In short, amazing store merchandising. For shoppers in ...

Catherine Dee

Search and Discovery writer

Introducing the new Algolia no-code data connector platform
engineering

Introducing the new Algolia no-code data connector platform

Ingesting data should be easy, but all too often, it can be anything but. Data can come in many different ...

Keshia Rose

Staff Product Manager, Data Connectivity

Customer-centric site search trends
e-commerce

Customer-centric site search trends

Everyday there are new messages in the market about what technology to buy, how to position your company against the ...

Piyush Patel

Chief Strategic Business Development Officer

What is online retail merchandising? An introduction
e-commerce

What is online retail merchandising? An introduction

Done any shopping on an ecommerce website lately? If so, you know a smooth online shopper experience is not optional ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

5 considerations for Black Friday 2023 readiness
e-commerce

5 considerations for Black Friday 2023 readiness

It’s hard to imagine having to think about Black Friday less than 4 months out from the previous one ...

Piyush Patel

Chief Strategic Business Development Officer

How to increase your sales and ROI with optimized ecommerce merchandising
e-commerce

How to increase your sales and ROI with optimized ecommerce merchandising

What happens if an online shopper arrives on your ecommerce site and: Your navigation provides no obvious or helpful direction ...

Catherine Dee

Search and Discovery writer

Mobile search UX best practices, part 3: Optimizing display of search results
ux

Mobile search UX best practices, part 3: Optimizing display of search results

In part 1 of this blog-post series, we looked at app interface design obstacles in the mobile search experience ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

Mobile search UX best practices, part 2: Streamlining search functionality
ux

Mobile search UX best practices, part 2: Streamlining search functionality

In part 1 of this series on mobile UX design, we talked about how designing a successful search user experience ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

Mobile search UX best practices, part 1: Understanding the challenges
ux

Mobile search UX best practices, part 1: Understanding the challenges

Welcome to our three-part series on creating winning search UX design for your mobile app! This post identifies developer ...

Vincent Caruana

Sr. SEO Web Digital Marketing Manager

Teaching English with Zapier and Algolia
engineering

Teaching English with Zapier and Algolia

National No Code Day falls on March 11th in the United States to encourage more people to build things online ...

Alita Leite da Silva

How AI search enables ecommerce companies to boost revenue and cut costs
ai

How AI search enables ecommerce companies to boost revenue and cut costs

Consulting powerhouse McKinsey is bullish on AI. Their forecasting estimates that AI could add around 16 percent to global GDP ...

Michelle Adams

Chief Revenue Officer at Algolia

What is digital product merchandising?
e-commerce

What is digital product merchandising?

How do you sell a product when your customers can’t assess it in person: pick it up, feel what ...

Catherine Dee

Search and Discovery writer

Scaling marketplace search with AI
ai

Scaling marketplace search with AI

It is clear that for online businesses and especially for Marketplaces, content discovery can be especially challenging due to the ...

Bharat Guruprakash

Chief Product Officer

The changing face of digital merchandising
e-commerce

The changing face of digital merchandising

This 2-part feature dives into the transformational journey made by digital merchandising to drive positive ecommerce experiences. Part 1 ...

Reshma Iyer

Director of Product Marketing, Ecommerce

What’s a convolutional neural network and how is it used for image recognition in search?
ai

What’s a convolutional neural network and how is it used for image recognition in search?

A social media user is shown snapshots of people he may know based on face-recognition technology and asked if ...

Catherine Dee

Search and Discovery writer

What’s organizational knowledge and how can you make it accessible to the right people?
product

What’s organizational knowledge and how can you make it accessible to the right people?

How’s your company’s organizational knowledge holding up? In other words, if an employee were to leave, would they ...

Catherine Dee

Search and Discovery writer

Adding trending recommendations to your existing e-commerce store
engineering

Adding trending recommendations to your existing e-commerce store

Recommendations can make or break an online shopping experience. In a world full of endless choices and infinite scrolling, recommendations ...

Ashley Huynh

Looking for something?

Pest – a PHP testing framework that goes above and beyond PHPUnit
facebookfacebooklinkedinlinkedintwittertwittermailmail

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.

Reducing the testing code base 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);
});

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);        
    } 
    // ...
}

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'
]);

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');

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();

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.

About the author
Peter Villani

Sr. Tech & Business Writer

linkedinmediumtwitter

Recommended Articles

Powered byAlgolia Algolia Recommend

How to improve site search with Algolia A/B testing
e-commerce

Loise Mercier

Resilience testing in production: test as you deploy
engineering

Xavier Grand

Engineering Manager

How to build a simulator for any feature — Part 2: Browser emulation
engineering

Jaden Baptista

Technical Writer