Write feature and unit tests with PHPUnit and Pest
✓Works with OpenClaudeYou are a Laravel testing expert. The user wants to write both feature and unit tests using PHPUnit and Pest in a Laravel application.
What to check first
- Run
php artisan --versionto confirm Laravel is installed - Check
phpunit.xmlexists in your project root and review the test directories configuration - Verify
composer require --dev phpunit/phpunitandcomposer require --dev pestphp/pest pestphp/pest-plugin-laravelare in yourcomposer.json
Steps
- Create a unit test file in
tests/Unit/usingphp artisan make:test UserTest --unitfor isolated logic testing - Create a feature test in
tests/Feature/usingphp artisan make:test AuthenticationTestfor HTTP requests and full stack testing - In unit tests, extend
Tests\TestCaseand test single classes likeUser::classmodel methods in isolation without database - In feature tests, use
$this->get(),$this->post(),$this->put(),$this->delete()to make HTTP requests and assert responses - Use
$this->actingAs($user)to authenticate within feature tests before making protected requests - Apply
RefreshDatabasetrait to reset database state between tests, or useWithFakerfor generating test data - Use Pest's syntax with
it()andexpect()functions for cleaner, more readable assertions if using Pest - Run
php artisan testto execute all tests, orphp artisan test --filter=UserTestfor specific tests
Code
<?php
namespace Tests\Unit;
use App\Models\User;
use PHPUnit\Framework\TestCase;
class UserTest extends TestCase
{
public function test_user_full_name_concatenation()
{
$user = new User([
'first_name' => 'John',
'last_name' => 'Doe',
]);
$this->assertEquals('John Doe', $user->fullName());
}
public function test_user_has_admin_role()
{
$user = new User(['role' => 'admin']);
$this->assertTrue($user->isAdmin());
}
}
namespace Tests\Feature;
use App\Models\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class AuthenticationTest extends TestCase
{
use RefreshDatabase;
public function test_user_can_login_with_valid_credentials()
{
$user = User::factory()->create([
'email' => 'test@example.com',
'password' => bcrypt('password123'),
]);
$response = $this->post('/login', [
'email' => 'test@example.com',
'password' => 'password123',
]);
$response->assertRedirect('/dashboard
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Laravel Skills
Other Claude Code skills in the same category — free to download.
Laravel Setup
Scaffold Laravel project with authentication and API
Laravel Eloquent
Write Eloquent models with relationships and scopes
Laravel Migration
Create database migrations with foreign keys and indexes
Laravel API Resource
Build REST API with resources and form requests
Laravel Queue
Set up job queues with Redis/database drivers
Laravel Livewire
Build reactive components with Laravel Livewire
Laravel Nova
Customize Laravel Nova admin panels
Want a Laravel skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.