Laravel Plugin

Overview

Source code: github.com/pestphp/pest-plugin-laravel

Although Pest is framework agnostic, it was built with Laravel in mind. The Laravel Plugin for Pest gives your direct access to Laravel's testing API in your test files.

Installation

Install the Laravel Plugin via the Composer package manager:

1composer require pestphp/pest-plugin-laravel --dev

Since Pest is a progressive testing framework, your current test suite will still work, even if you run it with the ./vendor/bin/pest console command.

Available Artisan Commands

pest:install

The pest:install Artisan command creates the tests/Pest.php file in your tests folder:

1php artisan pest:install

As described on the Underlying Test Case section, the Pest.php file is the place where all your uses should live.

pest:test

The pest:test <name> Artisan command creates a new test in the tests/Feature folder:

1php artisan pest:test UsersTest

Optionally, you can provide the --unit option to create the given test under the tests/Unit folder:

1php artisan pest:test UsersTest --unit

pest:dataset

The pest:dataset Artisan command creates a new dataset in the tests/Datasets folder:

1php artisan pest:dataset Emails

As detailed on the Datasets section, datasets allow you to run the same test multiple times with different data.

Available functions

Laravel provides several assertions for your feature tests. You may access those assertions as usual with the $this variable, or may import and use the functions under the Pest\Laravel namespace to have easy access Laravel's testing API. As example, the get function makes a GET request into the application:

1use function Pest\Laravel\get;
2
3it('has a welcome page', function () {
4 get('/')->assertStatus(200);
5
6 // Same as:
7 $this->get('/')->assertStatus(200);
8});

Using Test Traits

Laravel includes multiple test traits that allow you to configure the setUp of your test suite.

As explained in the Underlying Test Case section, the usage of traits in Pest is possible with the uses function.

As example, here is how you can use the RefreshDatabase trait in Pest:

1<?php
2
3use App\User;
4use Illuminate\Foundation\Testing\RefreshDatabase;
5
6uses(RefreshDatabase::class);
7
8beforeEach(fn () => User::factory()->create());
9
10it('has users')->assertDatabaseHas('users', [
11 'id' => 1,
12]);

Keep in mind that you can avoid repeating the uses(RefreshDatabase::class) line in each of your tests by binding it in your Pest.php file.

Tests without description

Also with the Laravel plugin, tests can be written without description. This lets you write even simpler tests than before:

1use function Pest\Laravel\get;
2
3get('/')->assertStatus(200);

Note that Pest will automatically generate a description for these tests:

1 get '/' assertStatus 200

To import multiple functions, wrap them in curly brackets like so:

1use function Pest\Laravel\{get, getJson};
2
3get('/')->assertStatus(200);
4
5getJson('api/posts')->assertStatus(200);

Once again, Pest will generate a description for each test:

1 get '/' assertStatus 200
2 getJson 'api/posts' assertStatus 200

Remember, for the full list of available Laravel testing methods, please refer to the Laravel documentation.

Laravel Dusk

Pest can also work seamlessly with Laravel Dusk. To get started, first follow Dusk's installation instructions.

Then, add the "Browser" test suite under the <testsuites> tag in your phpunit.xml file:

1<testsuites>
2 ...
3 <testsuite name="Browser">
4 <directory suffix="Test.php">./tests/Browser</directory>
5 </testsuite>
6</testsuites>

Finally, instruct Pest to use DuskTestCase as the base test case for the Browser directory by adding the following code to your Pest.php file:

1use Tests\DuskTestCase;
2
3uses(DuskTestCase::class)->in('Browser');

That's it! Now you can write Dusk tests using Pest's syntax:

1<?php
2
3use Laravel\Dusk\Browser;
4
5it('has homepage', function () {
6 $this->browse(function (Browser $browser) {
7 $browser->visit('/')
8 ->assertSee('Pest');
9 });
10});

Note: You can still use Dusk's regular class-based syntax as you can do with any other tests.

Now, when you run pest you will see your Dusk tests as well.

Note: As for now, the Laravel plugin doesn't support Dusk's functions, so you'll have to use them with the $this variable as the example above shows.


Next section: Livewire →