مقاله

نصب Pest، syntax it/expect، Pest Laravel plugin و اولین تست — readable تر از PHPUnit کلاسیک.

شروع تست با Pest در Laravel — جایگزین مدرن PHPUnit

شروع تست با Pest در Laravel — جایگزین مدرن PHPUnit

Laravel 12 به‌طور پیش‌فرض با Pest می‌آید — و خوب است. syntax functional با it() و expect() تست‌ها را کوتاه و readable می‌کند. بعد از سال‌ها PHPUnit class، تیم‌های جدید با Pest سریع‌تر تست می‌نویسند — pestphp.com.

نصب (اگر نبود)

composer require pestphp/pest --dev --with-all-dependencies
composer require pestphp/pest-plugin-laravel --dev
php artisan pest:install

اولین تست

// tests/Feature/ExampleTest.php
it('returns successful response', function () {
    $response = $this->get('/');
    $response->assertStatus(200);
});

بدون class و public function — فقط closure.

expect API

expect(true)->toBeTrue();
expect([1, 2])->toHaveCount(2);
expect('Laravel')->toContain('Lar');

RefreshDatabase

uses(RefreshDatabase::class);

it('creates a post', function () {
    $user = User::factory()->create();
    $this->actingAs($user);
    // ...
});

Factory: راهنما.

اجرای تست

php artisan test
./vendor/bin/pest
./vendor/bin/pest --filter=post

تفاوت Pest و PHPUnit

Pest روی PHPUnit ساخته شده — همان engine. syntax متفاوت؛ می‌توانید هر دو داشته باشید.

ساختار پیشنهادی

tests/Feature/PostTest.php
tests/Unit/PostServiceTest.php

Feature = HTTP/DB؛ Unit = کلاس ایزوله — Feature، Unit.

fake helpers

Event::fake();
Bus::fake();
Mail::fake();

در Event و Job.

جمع‌بندی

تست habit است نه بعدthought. از چک‌لیست پروژه شروع کنید. CI بعداً.

فرهنگ تست در تیم