Configuring Tests
The Pest.php file is a configuration file 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 may define Global Hooks or Custom Expectations within this file, its primary purpose is to specify the base test class used across your test suite.
When using Pest, the $this variable available within the closures you provide to test functions is bound to a specific test case class, which is typically PHPUnit\Framework\TestCase. This ensures that test cases written in Pest's functional style may 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\TestCase3 4 $this->assertTrue(true);5});
However, you may associate a specific folder, or even your entire test suite, with another base test case class, thereby changing the value of $this within your tests. To accomplish this, you may use the pest() function together with the in() method within your Pest.php configuration file:
1// tests/Pest.php2pest()->extend(Tests\TestCase::class)->in('Feature');3 4// tests/Feature/ExampleTest.php5it('has home', function () {6 echo get_class($this); // \Tests\TestCase7});
In addition, Pest supports glob patterns in the in() method, allowing you to specify multiple directories or files with a single pattern. Glob patterns are string representations that match various file paths, much like wildcards. If you are unfamiliar with glob patterns, you may refer to the PHP manual:
1// tests/Pest.php2pest()->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.
For a more complex example, you may use a pattern to match multiple directories across different modules while applying multiple test case classes and traits:
1// tests/Pest.php2pest()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 defined as public or protected in your base test case class may 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.php13pest()->extend(TestCase::class)->in('Feature');14 15// tests/Feature/ExampleTest.php16it('has home', function () {17 $this->performThis();18});
A trait may be linked to a test or folder, much like a class. For instance, in Laravel, you may use 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<?php2 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 may use the pest()->extend() and pest()->use() methods within that specific test file, omitting the in() method:
1pest()->extend(Tests\MySpecificTestCase::class);2 3it('has home', function () {4 echo get_class($this); // \Tests\MySpecificTestCase5});
Next, one of the features available to you when configuring your test suite is the ability to group folders. Once in place, this feature allows you to filter the tests you execute using the --group option: Grouping Tests