MANUAL

Testing your app with Pest

Testing your app with Pest

A test is a piece of code that checks whether your other code is doing what it’s supposed to. If your AI assistant changes something, the tests will show within a few seconds whether anything else has gone wrong.

Pest is a PHP testing tool with a simple syntax. New Laravel projects can have Pest included as part of the installation.

01

Step 1: Install (if you don’t already have it)

composer require pestphp/pest --dev --with-all-dependencies
./vendor/bin/pest --init

Running tests:

php artisan test
# of
./vendor/bin/pest

02

Step 2: the difference between types of tests

Type Test Speed Folder
Unit test A single function or class, independent of the rest Very fast tests/Unit
Feature test A complete sequence: request → controller → database → response Fast tests/Feature
End-to-end (E2E) The app in a real browser Slow using, for example, Playwright or Pest browser tests

Start with feature tests: they catch most of the issues in a standard web app.

03

Step 3: your first test

php artisan make:test FactuurTest --pest
use App\Models\User;
use App\Models\Klant;

it('maakt een factuur aan', function () {
    $user = User::factory()->create();
    $klant = Klant::factory()->create();

    $this->actingAs($user)
        ->post('/facturen', ['klant_id' => $klant->id, 'bedrag' => 121.00])
        ->assertRedirect();

    expect($klant->facturen()->count())->toBe(1);
});

it('weigert een negatief bedrag', function () {
    $user = User::factory()->create();

    $this->actingAs($user)
        ->post('/facturen', ['klant_id' => 1, 'bedrag' => -5])
        ->assertSessionHasErrors('bedrag');
});

In phpunit.xml, set up a separate test database (for example, an in-memory SQLite database) so that tests never affect your real data. Use RefreshDatabase to ensure that each test starts with an empty database.

04

Step 4: Let your AI assistant write the test first

This is called test-driven development (TDD):

  1. Write (or have someone write) a test that describes what you want. It should fail at first (red).
  2. Let the assistant write the code until the test passes (green).
  3. Tidy up the code without causing the test to fail (refactor).

Put it this way:

First, write a PEST test: a customer without an email address should not receive a reminder. Show that it fails. Only then should you amend the code.

The skills test-driven-development and tdd-laravel ensure your assistant does this consistently. With verification-before-completion, it only reports ‘done’ once the tests have actually passed.

05

Step 5: Running tests automatically

Run your tests with every push using GitHub Actions. You’ll then see a green tick or a red cross for every pull request. See Putting your Laravel app online.

06

A few terms

07

Need help?

Would you like someone to check whether your tests cover the important aspects? Take a look at AI code review.

08

Skills

09

Related services