Skip to content

Latest commit

 

History

History
158 lines (111 loc) · 4.1 KB

File metadata and controls

158 lines (111 loc) · 4.1 KB

How to use PEST Testing Framework in Laravel

Requirement

  • PHP 8.1+ or higher installed on your system.

The first step : is to require Pest as a "dev" dependency in your project by running the following commands on your command line.

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

Secondly : you'll need to initialize Pest in your current PHP project. This step will create a configuration file named Pest.php at the root level of your test suite, which will enable you to fine-tune your test suite later.

./vendor/bin/pest --init

Finally, you can run your tests by executing the pest command.

./vendor/bin/pest

You can run individually

./vendor/bin/pest tests/Feature/LoginTest.php

Common Error

Error-1 :

When you try to test relevant with your database. First you'll see an error like below -

Don't worry just update your phpunit.xml file with your database related necessary credentials.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
  <coverage/>
  <testsuites>
    <testsuite name="Unit">
      <directory suffix="Test.php">./tests/Unit</directory>
    </testsuite>
    <testsuite name="Feature">
      <directory suffix="Test.php">./tests/Feature</directory>
    </testsuite>
  </testsuites>
  <php>
    <server name="APP_ENV" value="testing"/>
    <server name="BCRYPT_ROUNDS" value="4"/>
    <server name="CACHE_DRIVER" value="array"/>
    <server name="DB_CONNECTION" value="mysql"/>
    <server name="DB_DATABASE" value="peoplepro_hrmandcrm"/>
    <server name="MAIL_DRIVER" value="array"/>
    <server name="QUEUE_CONNECTION" value="sync"/>
    <server name="SESSION_DRIVER" value="array"/>
  </php>
  <source>
    <include>
      <directory suffix=".php">./app</directory>
    </include>
  </source>
</phpunit>

Uses

1. Login Setup

You can set login test in your application. First create the File - /tests/Feature/LoginTest.php and write the below code.

Test 1 : Get Login Page

<?php
it('login Page Load', function () {

    $response = $this->get('/login');

    $response->assertStatus(200);
});

Test 2 : Login with the credentials

test('login with username and password', function () {

    $this->post('/login',[
        'username' => 'admin',
        'password' => 'admin',
    ]);

    $this->assertAuthenticated();
});

Test 3 : Redirect Dashboard after login.

it('After login, it will redirect to Admin Dashboard page', function () {
    $this->post('/login',[
        'username' => 'admin',
        'password' => 'admin',
    ]);
    $this->get(url('admin/dashboard'))->assertOk();
});

Now run the below command -

./vendor/bin/pest tests/Feature/LoginTest.php

Output :

Pest hooks are similar to the steps that -

You might take when preparing a meal - first, you gather and prepare the ingredients, then you cook the meal, and finally, you clean up after yourself.

In the same way, hooks allow you to perform specific actions before and after each test or file, such as setting up test data, initializing the test environment, or cleaning up resources after the tests are complete.

Here's a list of the hooks that are available in Pest:

  1. beforeEach()
  2. afterEach()
  3. beforeAll()
  4. afterAll()


Some Theoretical Concept