// docs / global-hooks

Global Hooks

As you may recall, hooks simplify your testing process and automate repetitive tasks that you perform before or after a test. However, when the same hooks are repeated across multiple test files, you may wish to define "global" hooks to avoid duplication. You may define global hooks within 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 may 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 may also be used within 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 run prior to the hooks defined in individual test files. Similarly, any after* hooks defined in the Pest.php configuration file will run after the hooks defined in individual test files.


Next, let's look at how to reduce duplication across your test suite by extracting reusable logic into custom helper functions: Custom Helpers