We noticed you're using an ad blocker

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

PEST - Make Laravel test easier

world cup 2022

FootballDesk

Get update standing EPL, UEFA, LaLiga and more

Get Now

Testing application is very much important for making a bug free application but it's a challenging task. The beginner developer most often leaves this term also. The Pest PHP testing framework makes the task easier and simpler. Now you can write the Laravel unit test, features test very easily. You will write less code and it will do more for you. It makes the test case more readable. If you are a beginner to writing the test case for your application or website then you don't have to worry after reading this post entirely with patience. Hope you will get confidence to write your application test case. Let's start the journey to write test case with PEST PHP testing framework.

Why writing test is so important?

If we do not write test cases for our application or website then the package update or framework version update will be a very difficult task because we have to manually test each and every page functions and features which is a very very tough task and sometimes it's impossible to do if the application is getting bigger and bigger.

If we write test cases during the development of our every function and features of the application then by running a simple test run command we can easily find out where the application is failing and we can easily fix that function or feature and make our application more stable and maintainable.

 

PEST requirements

  • Need PHP 7.3 or more.
  • PHPUnit ^9.0
  • Collision dependency ^5.0

 

PEST Installation

PHPUnit dependency ^9.0

composer require phpunit/phpunit:"^9.0" --dev --update-with-dependencies

Collision Dependency ^5.0

composer require nunomaduro/collision:"^5.0" --dev --update-with-dependencies

Install PEST as a dev dependency

composer require pestphp/pest --dev

Now run the pest install command

php artisan pest:install

Our installation is finished. If everything installed correctly then you will get test output for default test cases by running php artisan test

You can watch the video for Laravel PEST installation to get a more clear idea.

 

How simple the PEST test cases?

Laravel default test case example

<?php

namespace Tests\Unit;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $this->assertTrue(true);
    }
}

Now look the same test in Pest framework.

it('Basic Test')->assertTrue(true);

 

Test Scenario

Writing test cases for application or website will be different according to your need and application context. Here for the demonstration, I am going to show you a test scenario for a blog site. Suppose in our blog site there 4 pages like home, blogs, blog details and category page. In other scenarios, there might be a lot of pages.

So I am writing the test cases for testing whether pages are responding without any exception or not. Let's write the test cases.

php artisan pest:test FrontendTest

after running this command a FrontendTest.php file will be created in the tests/Feature directory. Write the test case for testing the pages are loading without any error.

├───Feature
│       FrontendTest.php
└───Unit
        ExampleTest.php
//tests/Feature/FrontendTest.php
use App\Post;

$post = Post::first();

it('home')
    ->get('/')
    ->assertStatus(200);

it('blog')
    ->get('/blogs')
    ->assertStatus(200);

it('blog-detail')
    ->get("/blogs/$post->slug")
    ->assertStatus(200);

it('category')
    ->get('/categories')
    ->assertStatus(200);

Run the command.

php artisan test --filter FrontendTest

If we run php artisan test then it will run all the unit test and features test. Here I have use filter flag for running only the FrontendTest cases.

pest-test.png

Hope this post will help you to learn how to test Laravel website or application using Pest php testing framework. If you find this post helpful then please share it other so that they can get helped.


Share on




Related Post - Latest Post


Tinkerpad - A minimal Laravel code editor

What's new in Laravel 9

Make laravel site super fast by page-cache!

Laravel maintenance mode bypass by Secret Route!

Laravel database backup automatically - cPanel shared hosting, VPS

Laravel Datatables - Ajax, Column, Buttons, Customization