Global Hooks

As previously discussed, hooks allow you to simplify your testing process and automate repetitive tasks that you may perform before or after a test. However, if the hooks are the same across multiple test files, you may wish to define "global" hooks to avoid code duplication. Global hooks are defined in your Pest.php configuration file.

For instance, if you need to perform some database operations before each test within the Feature folder, you may use the beforeEach() hook within your Pest.php configuration file.

1pest()->extend(TestCase::class)->beforeEach(function () {
2 // Interact with your database...
3})->group('integration')->in('Feature');

In addition, you can define global hooks that will run before or after your entire test suite, regardless of the folder or group.

1pest()->beforeEach(function () {
2 // Interact with your database...
3});

In fact, any of the hooks mentioned in the hooks documentation can also be used in your Pest.php configuration file.

1pest()->extend(TestCase::class)->beforeAll(function () {
2 // Runs before each file...
3})->beforeEach(function () {
4 // Runs before each test...
5})->afterEach(function () {
6 // Runs after each test...
7})->afterAll(function () {
8 // Runs after each file...
9})->group('integration')->in('Feature');

Any before* hooks defined in the Pest.php configuration file will be executed prior to hooks defined in individual test files. Similarly, any after* hooks specified in the Pest.php configuration file will be executed after any hooks defined in individual test files.


When setting up a test suite, it may be necessary to mock certain functionality or objects in order to isolate the code being tested and to simulate certain conditions or behaviors. This can be done through the use of mocking libraries or frameworks, such as Mockery: Mocking