All You Need To Know To Start Using Pest in Laravel Application

Laravel 1 min read

Table of Content

    Introduction

    We discussed previously PHPUnit Framework and the type of testing, why we do tests, and all that kind of stuff.

    Today, we will take a closer step into Pest PHP framework and understand why it's amazing.

    Prerequisite

    You need to know the following:

    • Need to know PHP

    Introduction To Pest

    Pest is a Testing Framework with a focus on simplicity. It was carefully crafted to bring the joy of testing to PHP.

    Installation

    1. First, install Pest via the Composer package manager:
    composer require pestphp/pest --dev --with-all-dependencies
    
    1. On Laravel, require the pest-plugin-laravel and run the pest:install Artisan command:
    composer require pestphp/pest-plugin-laravel --dev
    
    php artisan pest:install
    
    1. On other projects, create a tests folder and run the pest --init command:
    ./vendor/bin/pest --init
    
    1. Finally, you can run Pest directly from the command line:
    ./vendor/bin/pest
    

    Pest PHP

    Your First Test

    Pest makes it easy to write tests. This section illustrates how to write a simple test suite with Pest, and what are the conventions you should use.

    The setup is very simple, and usually looks like this:

    tests
        - Unit/ComponentTest.php <--
        - Feature/HomeTest.php <--
    phpunit.xml
    

    To write a test, create a file in the Unit or Feature directory, and make sure its filename ends with the ..Test.php suffix. Then, all you need inside this file is a function that runs your test:

    <?php
    test('has home', function () {
        // ..
    });
    
    // or
    it('has home', function () {
        // ..
    });
    

    Note: Pest will only run a test file if its name ends with the suffix set in your phpunit.xml.

    API Reference

    Now, on to the API reference. Pest offers you two functions to write your tests: test() & it(). Use the one that best fits your test naming convention, or both. They share the same behavior & syntax:

    test()

    The test function adds the given closure as test. The first argument is the test description; the second argument is a closure that contains the test expectations:

    test('asserts true is true', function () {
        $this->assertTrue(true);
    
        expect(true)->toBeTrue();
    });
    

    Here is what this example test will return:

    ✓ asserts true is true
    

    it()

    The it function adds the given closure as test. The first argument is the test description; the second argument is a closure that contains the test expectations:

    it('asserts true is true', function () {
        $this->assertTrue(true);
    
        expect(true)->toBeTrue();
    });
    

    Here is what this example test will return:

    ✓ it asserts true is true
    

    Note: Notice how, when using the it function, your test name gets prepended with 'it' in the returned description.

    Conclusion

    Today We Learned about Pest Framework and how to use it properly with your environment.

    If you have any questions, you can use comment them below, and I'll be more than happy to answer them.

    Hope you learned something useful today!


    Related Tags

    About the Author

    Oussama's Profile Picture
    Oussama
    Full Stack Web Developer

    I'm a full stack web developer and telecommunications engineer who love to share knowledge and build stuff online!


    Comments

    Join Our Newsletter

    Subscribe to Our Newsletter and never miss our offers, latest news, Articles, etc.

    Our news letter sent once a week, every tuesday.