Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.

15 St Margarets, NY 10033
(+381) 11 123 4567



Given-When-Then Plugin for Pest | Laravel News

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 AppExceptionsBlockedUserException;

2use AppModelsUser;

3use function PestGwtscenario;

4use function PestLaravelassertDatabaseHas;


6scenario('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 ]));


14scenario('activate blocked user')

15 ->given(fn() => User::factory()->blocked()->create())

16 ->when(fn(User $user) => $user->activate())

17 ->throws(BlockedUserException::class);

The repo includes more examples of GWT-style tests for Pest to help you get started. You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Credit: Source link

Previous Next
Test Caption
Test Description goes like this