# 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.

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

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

Running tests:

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

## 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.

## Step 3: your first test

```bash
php artisan make:test FactuurTest --pest
```

```php
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.

## 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.

## 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*.

## A few terms

- **Assertion:** the check in a test (`assertStatus(200)`, `expect(...)->toBe(...)`).
- **Factory:** generates test data (`User::factory()`).
- **Mock / stub:** a fake version of an external service, such as a payment provider, so that your test does not actually make a payment.
- **Regression:** something that used to work but is now broken. Tests are your safety net.

## Need help?

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

## Skills

- [Test-Driven Development (TDD)] (/nl/skills/test-driven-development)
- [Test-Driven Development] (/nl/skills/tdd-laravel)
- [Laravel 13 Testing — Pest PHP 4 & PHPUnit 12] (/nl/skills/laravel-testing)
- [E2E Testing Patterns] (/nl/skills/e2e-testing-patterns)
- [Web Application Testing] (/nl/skills/webapp-testing)
- [Verification Before Completion] (/nl/skills/verification-before-completion)

## Related services

- [Beoordeling van AI-code] (/nl/services#service-05)
- [Maatwerk software en webapplicaties] (/nl/services#service-03)

