When working with Laravel, developers need an efficient and expressive testing framework that doesn’t get in the way of the code’s natural flow. Enter Laravel Pest, a simple yet powerful testing framework that integrates beautifully with Laravel. In this blog, we’ll dive into how to use pestphp/pest-plugin-laravel, compare Laravel Pest vs PHPUnit, and explore how you can extend Pest’s functionality with plugins like pestphp/pest-plugin-faker.

Table of Contents


What is Laravel Pest?

Laravel Pest is a PHP testing framework designed to simplify the testing experience in Laravel applications. It provides an elegant and expressive syntax, making it easier for developers to write and maintain tests. Pest is built on top of PHPUnit, meaning you get all the features and capabilities of PHPUnit, but with a streamlined and more intuitive syntax.

The beauty of Laravel Pest lies in its simplicity. Instead of writing verbose tests with PHPUnit’s traditional syntax, Pest allows you to focus on what really matters: testing your application.


What is pestphp/pest-plugin-laravel?

The pestphp/pest-plugin-laravel is an official plugin that enhances the integration between Pest and Laravel. This plugin is designed to make testing Laravel applications even easier by providing a set of helper functions, Artisan commands, and seamless integration with Laravel’s built-in testing capabilities.

With pestphp/pest-plugin-laravel, developers can take full advantage of Pest’s expressive syntax while working with Laravel. It provides essential tools for Laravel-specific testing scenarios, such as database assertions, HTTP requests, and authentication.

Key Features of pestphp/pest-plugin-laravel

  • Artisan Commands: It includes custom commands like php artisan pest:test to run tests and php artisan pest:dataset to generate test data sets.
  • Seamless Laravel Integration: Functions like actingAs, get, post, delete, and others work just like Laravel’s built-in testing methods but with more concise syntax.
  • Laravel Assertions: The plugin provides easy access to Laravel’s testing assertions like assertStatus, assertSee, and assertDatabaseHas.
  • Streamlined Syntax: Pest allows you to eliminate boilerplate code, making your tests more readable and maintainable.

Key Features of Laravel Pest

Here are some of the key features that make Laravel Pest the preferred choice for testing Laravel applications:

  1. Expressive Syntax: Pest eliminates the need for cumbersome $this->assert* methods. Instead, you can use simple, readable functions. test('home page is accessible')->get('/')->assertStatus(200);
  2. Improved Test Writing: Pest’s simple syntax improves the readability of your tests, reducing boilerplate code and making your test files clean and easy to maintain.
  3. Laravel-Specific Helpers: With the pestphp/pest-plugin-laravel, Pest includes various helpers tailored for Laravel, such as actingAs(), get(), and post(). This integration makes it feel like you’re using native Laravel testing, but with cleaner syntax.
  4. Enhanced Assertions: Pest supports Laravel’s built-in assertions like assertDatabaseHas(), assertSee(), and more, ensuring your tests remain robust and comprehensive.
  5. Support for Parallel Testing: Pest allows you to run tests in parallel, which significantly speeds up the testing process, especially in large projects.

Installing Laravel Pest

Installing Laravel Pest along with the pestphp/pest-plugin-laravel is straightforward. Follow these steps to get started:

Step 1: Remove PHPUnit (if previously installed)

Since Pest is built on top of PHPUnit, it’s best to remove any previous versions of PHPUnit before installing Pest.

composer remove phpunit/phpunit

Step 2: Install Pest and the Laravel Plugin

Run the following commands to install Pest and the pestphp/pest-plugin-laravel.

composer require pestphp/pest --dev --with-all-dependencies
composer require pestphp/pest-plugin-laravel --dev

Step 3: Initialize Pest

Next, you need to initialize Pest in your Laravel project. This step will set up the necessary configuration files and directories.

php artisan pest:install

Laravel Pest vs PHPUnit: A Detailed Comparison

While Laravel Pest and PHPUnit serve the same purpose—testing PHP code—there are key differences between them.

Laravel Pest:

  • Expressive and Clean Syntax: Pest removes boilerplate code and offers a more readable, less verbose syntax. It focuses on making the test-writing process as straightforward as possible.
  • Built on PHPUnit: Pest is built on top of PHPUnit, so it inherits all of PHPUnit’s powerful features while simplifying the syntax.
  • Fewer Lines of Code: With Pest, you can write tests in fewer lines compared to PHPUnit.
  • Plugins and Extensions: Pest comes with an ecosystem of plugins, including pestphp/pest-plugin-laravel and pestphp/pest-plugin-faker, which further enhance its capabilities.

PHPUnit:

  • Established and Comprehensive: PHPUnit has been the standard for testing in PHP for years and offers a comprehensive set of features, including mock objects and test runners.
  • Verbose Syntax: Writing tests in PHPUnit involves more boilerplate code, especially when dealing with assertions.
  • More Complex Setup: PHPUnit requires more setup and configuration compared to Pest, making it less user-friendly for beginners.

In summary, while both frameworks provide powerful testing capabilities, Laravel Pest stands out for its simplicity and Laravel-specific integration, making it ideal for Laravel developers who want to streamline their testing process.


Using pestphp/pest-plugin-laravel in Laravel Projects

The pestphp/pest-plugin-laravel is the perfect companion for Laravel developers using Pest. It simplifies testing by offering Laravel-specific helper functions and commands. Here’s how to use it effectively:

Example Test with Pest

use function Pest\Laravel\{get, post};

test('home page is accessible')
    ->get('/')
    ->assertStatus(200);

test('user can create a post')
    ->actingAs($user)
    ->post('/posts', ['title' => 'New Post', 'body' => 'Post body'])
    ->assertStatus(201)
    ->assertDatabaseHas('posts', ['title' => 'New Post']);

In the above example, you can see how easy it is to make HTTP requests and assert database changes with Pest’s intuitive syntax. You no longer need to deal with $this->get() or $this->actingAs(), as the plugin streamlines the process.


Enhancing Pest with pestphp/pest-plugin-faker

Another great plugin to extend Pest’s functionality is pestphp/pest-plugin-faker. This plugin integrates Faker, a library for generating fake data, with Pest.

With pestphp/pest-plugin-faker, you can easily generate fake data for testing, making your tests more realistic and versatile.

Example Usage of pestphp/pest-plugin-faker:

use Faker\Factory as Faker;

test('create fake user')
    ->faker(fn () => Faker::create())
    ->post('/users', [
        'name' => $faker->name,
        'email' => $faker->email,
    ])
    ->assertStatus(201);

This plugin makes it simple to incorporate fake data into your tests, allowing you to create more dynamic and comprehensive test scenarios.


Conclusion

In conclusion, Laravel Pest is a game-changer for Laravel developers who want to write tests that are both easy to read and maintain. By using pestphp/pest-plugin-laravel, you can take full advantage of Laravel’s built-in testing features while enjoying Pest’s streamlined syntax.

Whether you’re transitioning from PHPUnit or just starting your testing journey, Laravel Pest offers an elegant solution that enhances productivity and improves test reliability. Combined with plugins like pestphp/pest-plugin-faker, you can make your tests even more powerful and realistic.

Ready to make the switch? Start using Laravel Pest today, and elevate your testing workflow!


References:

Categorized in: