Setup and Teardown

Overview

In your tests, you may want to run some code before and after each test or file. In this section, we'll discuss the globally available functions that allow you to do that.

Setup and Teardown

Here you can find a list of available setup/teardown functions.

beforeEach()

The function beforeEach() runs the given closure before each test in the current file.

It's the equivalent to setUp in PHPUnit.

1<?php
2 
3beforeEach(function () {
4 echo 'beforeEach';
5});
6 
7test('foo', function () {
8 echo 'test foo';
9});
10 
11test('bar', function () {
12 echo 'test bar';
13});
14 
15// beforeEach
16// test foo
17// beforeEach
18// test bar

As usual, the $this variable in the beforeEach function is bound to the current Test Case object. Therefore, you can share data with both it and test functions.

1beforeEach(function () {
2 $this->hey = 'artisan';
3});
4 
5it('has artisan', function () {
6 echo $this->hey;
7});
8 
9// artisan

afterEach()

The function afterEach() runs the given closure after each test in the current file.

It's the equivalent to tearDown in PHPUnit.

1<?php
2 
3afterEach(function () {
4 echo 'afterEach';
5});
6 
7test('foo', function () {
8 echo 'test foo';
9});
10 
11test('bar', function () {
12 echo 'test bar';
13});
14 
15// test foo
16// afterEach
17// test bar
18// afterEach

beforeAll()

The function beforeAll() runs the given closure before all tests in the current file.

It's the equivalent to the @beforeClass annotation in PHPUnit.

1<?php
2 
3beforeAll(function () {
4 echo 'beforeAll';
5});
6 
7test('foo', function () {
8 echo 'test foo';
9});
10 
11test('bar', function () {
12 echo 'test bar';
13});
14 
15// beforeAll
16// test foo
17// test bar

afterAll()

The function afterAll() runs the given closure after all tests in the current file.

It's the equivalent to the @afterClass annotation in PHPUnit.

1<?php
2 
3afterAll(function () {
4 echo 'afterAll';
5});
6 
7test('foo', function () {
8 echo 'test foo';
9});
10 
11test('bar', function () {
12 echo 'test bar';
13});
14 
15// test foo
16// test bar
17// afterAll

Reusable (Shared) Setup and Teardown

At some point, you may need (or want) to share some kind of test scenario setup or teardown procedure. Doing this is very easy, using the exact same tools used in the previous section, only this time we combine it with the uses() function!

1// Pest.php
2uses()
3 ->beforeEach(fn () => $this->actingAs(User::first()))
4 ->in('Feature/Dashboard');

All setup & teardown methods described on this page are available for reuse/sharing like this! beforeEach() is only used as one example.

Example

To understand the order of execution of all those functions, let's take a look at the example below:

1beforeAll(fn () => dump('beforeAll'));
2afterAll(fn () => dump('afterAll'));
3 
4beforeEach(fn () => dump('beforeEach'));
5afterEach(fn () => dump('afterEach'));
6 
7test('example 1', fn () => dump('test foo'));
8test('example 2', fn () => dump('test bar'));
9 
10// "beforeAll"
11// "beforeEach"
12// "test foo"
13// "afterEach"
14// "beforeEach"
15// "test bar"
16// "afterEach"
17// "afterAll"

Again! Except this time we will use all the global (reusable/shared hooks) we possibly can, and combine them with the local hooks in the previous example to show execution order.

1// tests/Pest.php
2uses()
3 ->beforeAll(fn () => dump(1))
4 ->beforeEach(fn () => dump(2))
5 ->afterEach(fn () => dump(3))
6 ->afterAll(fn () => dump(4))
7 ->in('Unit');
1// tests/Unit/MyTest.php
2uses()
3 ->beforeAll(fn () => dump('a'))
4 ->beforeEach(fn () => dump('b'))
5 ->afterEach(fn () => dump('c'))
6 ->afterAll(fn () => dump('d'));
7 
8beforeAll(fn () => dump('i'));
9beforeEach(fn () => dump('ii'));
10afterEach(fn () => dump('iii'));
11afterAll(fn () => dump('iv'));
12 
13test('order', fn () => dump('foo'));
14 
15// 1
16// a
17// i
18// 2
19// b
20// ii
21// foo
22// 3
23// c
24// iii
25// 4
26// d
27// iv

Next section: Higher Order Tests →