Given-When-Then (GWT) is a plugin that brings behavioral style testing to Pest:
GWT is a simple API that allows you to structure your tests focused on the behavior. Given-When-Then separation makes the test easier to understand at a glance.
Behavior-driven testing using GWT can help clarify three distinct steps while testing code, which includes 1) arranging the application state (given), 2) the "act" phase (when), and 3) the outcome phase (then) which asserts the expected outcome of the previous steps:
1use App\Exceptions\BlockedUserException;2use App\Models\User;3use function Pest\Gwt\scenario;4use function Pest\Laravel\assertDatabaseHas;56scenario('activate user')7 ->given(fn() => User::factory()->create())8 ->when(fn(User $user) => $user->activate())9 ->then(fn(User $user) => assertDatabaseHas('users', [10 'id' => $user->id,11 'activated' => true,12 ]));1314scenario('activate blocked user')15 ->given(fn() => User::factory()->blocked()->create())16 ->when(fn(User $user) => $user->activate())17 ->throws(BlockedUserException::class);