Configuring Tests

The Pest.php file is a configuration file that is used to define your test suite setup. This file is located in the tests directory of your project and is automatically loaded by Pest when you run your tests. Although you can define Global Hooks or Custom Expectations within this file, its primary purpose is to specify the base test class utilized in your test suite.

When using Pest, the $this variable available within closures provided to test functions is bound to a specific test case class, which is typically PHPUnit\Framework\TestCase. This guarantees that the test cases written in Pest's functional style can access the underlying assertion API of PHPUnit, simplifying collaboration with other developers who are more familiar with the PHPUnit testing framework.

1it('has home', function () {
2 echo get_class($this); // \PHPUnit\Framework\TestCase
3 
4 $this->assertTrue(true);
5});

However, you may associate a specific folder or even your entire test suite with another base test case class, thus changing the value of $this within tests. To accomplish this, you can utilize the pest() function and the in() method within your Pest.php configuration file.

1// tests/Pest.php
2pest()->extend(Tests\TestCase::class)->in('Feature');
3 
4// tests/Feature/ExampleTest.php
5it('has home', function () {
6 echo get_class($this); // \Tests\TestCase
7});

Additionally, Pest supports glob patterns in the in() method. This allows you to specify multiple directories or files with a single pattern. Glob patterns are string representations that can match various file paths, like wildcards. If you are unfamiliar with glob patterns, refer to the PHP manual here.

1// tests/Pest.php
2pest()->extend(Tests\TestCase::class)->in('Feature/*Job*.php');
3 
4// This will apply the Tests\TestCase to all test files in the "Feature" directory that contains "Job" in their filename.

Another more complex example would be using a pattern to match multiple directories in different modules while applying multiple test case classes and traits:

1// tests/Pest.php
2pest()
3 ->extend(DuskTestCase::class)
4 ->use(DatabaseMigrations::class)
5 ->in('../Modules/*/Tests/Browser');
6 
7// This will apply the DuskTestCase class and the DatabaseMigrations trait to all test files within any module's "Browser" directory.

Any method that is defined as public or protected in your base test case class can be accessed within the test closure.

1use PHPUnit\Framework\TestCase as BaseTestCase;
2 
3// tests/TestCase.php
4class TestCase extends BaseTestCase
5{
6 public function performThis(): void
7 {
8 //
9 }
10}
11 
12// tests/Pest.php
13pest()->extend(TestCase::class)->in('Feature');
14 
15// tests/Feature/ExampleTest.php
16it('has home', function () {
17 $this->performThis();
18});

A trait can be linked to a test or folder, much like classes. For instance, in Laravel, you can employ the RefreshDatabase trait to reset the database prior to each test. To include the trait in your test, pass the trait's name to the pest()->use() method.

1<?php
2 
3use Tests\TestCase;
4use Illuminate\Foundation\Testing\RefreshDatabase;
5 
6pest()->extend(TestCase::class)->use(RefreshDatabase::class)->in('Feature');

To associate a particular test with a specific test case class or trait, you can utilize the pest()->extend() and pest()->use() methods within that specific test file, omitting the use of the in() method.

1pest()->extend(Tests\MySpecificTestCase::class);
2 
3it('has home', function () {
4 echo get_class($this); // \Tests\MySpecificTestCase
5});

Next, one of the features available to you when configuring your test suite is the ability to group folders. When utilized, this feature allows you to filter your executed tests using the --group option: Grouping Tests