Naar de inhoud
Recognized by Laravel Match je project Contact

Gebruik PHPUnit of Pest voor testen

Voor het laatst bijgewerkt op:

Introductie

Laravel biedt first-class ondersteuning voor zowel PHPUnit als Pest als testframeworks. PHPUnit is het traditionele, class-gebaseerde testframework dat al vele jaren de standaard is in PHP, terwijl Pest een moderne, function-gebaseerde aanpak biedt met een eenvoudigere syntax. Beide frameworks worden officieel ondersteund en integreren naadloos met de testfunctionaliteit van Laravel.

Waarom

  • Officiële ondersteuning: Beide frameworks worden officieel onderhouden en gedocumenteerd door Laravel
  • Consistente tooling: De testhelpers van Laravel werken identiek met beide frameworks
  • Community-standaarden: Het gebruik van deze frameworks garandeert compatibiliteit met community-packages en best practices
  • Ingebouwde assertions: Beide bieden uitgebreide assertion-libraries die specifiek zijn ontworpen voor Laravel-applicaties
  • Eenvoudige opzet: Laravel wordt vooraf geconfigureerd geleverd met PHPUnit, en Pest kan met minimale configuratie worden toegevoegd
  • Continue verbetering: Beide frameworks ontvangen regelmatig updates en verbeteringen samen met Laravel-releases

Geschikt voor

  • Alle Laravel-applicaties die geautomatiseerd testen vereisen
  • Projecten waar teamleden de voorkeur geven aan traditioneel testen in xUnit-stijl (PHPUnit)
  • Projecten waar teamleden de voorkeur geven aan moderne, function-gebaseerde testsyntax (Pest)
  • Applicaties die moeten integreren met de testhelpers van Laravel (database factories, HTTP-testen, etc.)
  • Teams die overstappen van andere PHP-frameworks die PHPUnit gebruiken
  • Nieuwe projecten die op zoek zijn naar een schone, expressieve testsyntax (Pest)

Minder geschikt voor

  • Applicaties met bestaande testsuites in andere PHP-testframeworks (hoewel migratie mogelijk is)
  • Projecten met specifieke eisen voor alternatieve testtools die niet compatibel zijn met de testarchitectuur van Laravel
  • Teams met een sterke voorkeur voor testframeworks buiten het PHP-ecosysteem (hoewel dit doorgaans zou wijzen op een geheel andere taalkeuze)

Voorbeelden

PHPUnit-voorbeeld

<?php

namespace Tests\Feature;

use Tests\TestCase;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;

class UserRegistrationTest extends TestCase
{
    use RefreshDatabase;

    public function test_users_can_register(): void
    {
        $response = $this->post('/register', [
            'name' => 'John Doe',
            'email' => '[email protected]',
            'password' => 'password',
            'password_confirmation' => 'password',
        ]);

        $response->assertRedirect('/dashboard');
        $this->assertDatabaseHas('users', [
            'email' => '[email protected]',
        ]);
    }
}

Pest-voorbeeld

<?php

use App\Models\User;

test('users can register', function () {
    $response = $this->post('/register', [
        'name' => 'John Doe',
        'email' => '[email protected]',
        'password' => 'password',
        'password_confirmation' => 'password',
    ]);

    $response->assertRedirect('/dashboard');
    expect(User::where('email', '[email protected]')->exists())->toBeTrue();
});

Kiezen tussen PHPUnit en Pest

Kies PHPUnit als:

  • Je team al bekend is met traditioneel testen in xUnit-stijl
  • Je een bestaand Laravel-project onderhoudt dat PHPUnit gebruikt
  • Je de voorkeur geeft aan een class-gebaseerde, objectgeoriënteerde teststructuur
  • Je compatibiliteit nodig hebt met oudere Laravel-versies (vóór Laravel 8)

Kies Pest als:

  • Je een nieuw project start en een moderne testsyntax wilt
  • Je team de voorkeur geeft aan functionele programmeerstijlen
  • Je minder boilerplate-code in je tests wilt
  • Je Laravel 8 of nieuwer gebruikt

Let op: Beide frameworks kunnen naast elkaar bestaan in hetzelfde project, wat een geleidelijke migratie mogelijk maakt indien gewenst.

Meer info

Skill

Laravel Boost Skill

Gebruik deze skill om de richtlijn rechtstreeks toe te passen met een AI-assistent.

Use this skill when a Laravel task touches this best practice. It is self-contained so it can be installed independently by Laravel Boost or another agent-skill system.

## Core Guidance

Laravel provides first-class support for both PHPUnit and Pest as testing frameworks. PHPUnit is the traditional, class-based testing framework that has been the standard in PHP for many years, while Pest offers a modern, function-based approach with a simpler syntax. Both frameworks are officially supported and integrate seamlessly with Laravel's testing features.

## Why It Matters

- **Official support**: Both frameworks are officially maintained and documented by Laravel
- **Consistent tooling**: Laravel's testing helpers work identically with both frameworks
- **Community standards**: Using these frameworks ensures compatibility with community packages and best practices
- **Built-in assertions**: Both provide comprehensive assertion libraries specifically designed for Laravel applications
- **Easy setup**: Laravel comes pre-configured with PHPUnit, and Pest can be added with minimal configuration
- **Continuous improvement**: Both frameworks receive regular updates and improvements alongside Laravel releases

## Apply When

- All Laravel applications that require automated testing
- Projects where team members prefer traditional xUnit-style testing (PHPUnit)
- Projects where team members prefer modern, function-based testing syntax (Pest)
- Applications that need to integrate with Laravel's testing helpers (database factories, HTTP testing, etc.)
- Teams transitioning from other PHP frameworks that use PHPUnit
- New projects looking for a clean, expressive testing syntax (Pest)

## Be Careful When

- Applications with existing test suites in other PHP testing frameworks (though migration is possible)
- Projects with specific requirements for alternative testing tools not compatible with Laravel's testing architecture
- Teams with strong preferences for testing frameworks outside the PHP ecosystem (though this would typically indicate a different language choice altogether)

## Canonical Source

- Full best practice: https://github.com/Dutch-Laravel-Foundation/best-practices/blob/main/testing/use-phpunit-or-pest-for-testing/BEST_PRACTICE.md
- Dutch translation: https://github.com/Dutch-Laravel-Foundation/best-practices/blob/main/testing/use-phpunit-or-pest-for-testing/translations/nl.md

## Workflow

1. Inspect the user's Laravel code before recommending changes.
2. Identify the narrow rule from this best practice that applies to the task.
3. Prefer Laravel's built-in conventions and documented APIs over custom abstractions.
4. Keep examples focused on this practice; reference other skills or practices when the task crosses boundaries.
5. Verify code changes with the project's available tests, linters, static analysis, or framework checks.

## Review Checklist

- The recommendation is Laravel-specific and grounded in this practice.
- Code examples use realistic Laravel file names, class names, and method names.
- The advice avoids mixing unrelated architecture, deployment, security, or testing topics.
- Related practices are mentioned when useful, but not re-explained in full.
- Dutch output, when requested, keeps framework and API names intact.