Feature & Unit Tests in PHP
From the Laravel cheat sheet · Testing · verified Jul 2026
Feature & Unit Tests
Writing and running tests
php
// Feature test
public function test_homepage_returns_successful_response()
{
$response = $this->get('/');
$response->assertStatus(200);
$response->assertSee('Welcome');
}
// Unit test
public function test_user_has_full_name()
{
$user = new User(['first_name' => 'John', 'last_name' => 'Doe']);
$this->assertEquals('John Doe', $user->fullName());
}
// Run tests
php artisan test
php artisan test --filter=UserTest💡 Use RefreshDatabase trait for database isolation
⚡ Mock external services to avoid dependencies
📌 Test both success and failure paths
🔥 Run tests in parallel for faster execution