Laravel Magazine
Write Better Laravel Tests with Pest PHP
Featured

Write Better Laravel Tests with Pest PHP

If you have ever looked at a wall of PHPUnit boilerplate and thought "there has to be a better way," there is. Pest PHP is a testing framework built on top of PHPUnit that gives you a more expressive, less ceremonious syntax for writing tests — and it ships as the default testing framework in new Laravel applications. This tutorial covers Pest from installation through the features that make it genuinely worth using over plain PHPUnit. Installation New Laravel projects include Pest by default. For existing projects, install it and run the migration command: composer require pestphp/pest --dev composer require pestphp/pest-plugin-laravel --dev ./vendor/bin/pest --init Run your tests with: php artisan test # or ./vendor/bin/pest The Basic Syntax In PHPUnit, every test is a class with a method. In Pest, a test is a function call: // PHPUnit class UserTest extends TestCase { public function test_a_user_can_be_created(): void { $user = User::factory()->create(); $this->assertInstanceOf(User...

Recent Articles

Latest Links

Stay Updated

Subscribe to our newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.

Weekly articles
We send a new issue of the newsletter every week on Friday.
No spam
We'll never share your email address and you can opt out at any time.