$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
LaravelintermediateNew

Laravel Testing

Share

Write feature and unit tests with PHPUnit and Pest

Works with OpenClaude

You 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 --version to confirm Laravel is installed
  • Check phpunit.xml exists in your project root and review the test directories configuration
  • Verify composer require --dev phpunit/phpunit and composer require --dev pestphp/pest pestphp/pest-plugin-laravel are in your composer.json

Steps

  1. Create a unit test file in tests/Unit/ using php artisan make:test UserTest --unit for isolated logic testing
  2. Create a feature test in tests/Feature/ using php artisan make:test AuthenticationTest for HTTP requests and full stack testing
  3. In unit tests, extend Tests\TestCase and test single classes like User::class model methods in isolation without database
  4. In feature tests, use $this->get(), $this->post(), $this->put(), $this->delete() to make HTTP requests and assert responses
  5. Use $this->actingAs($user) to authenticate within feature tests before making protected requests
  6. Apply RefreshDatabase trait to reset database state between tests, or use WithFaker for generating test data
  7. Use Pest's syntax with it() and expect() functions for cleaner, more readable assertions if using Pest
  8. Run php artisan test to execute all tests, or php artisan test --filter=UserTest for 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

Quick Info

CategoryLaravel
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
laraveltestingphpunit

Install command:

curl -o ~/.claude/skills/laravel-testing.md https://clskills.in/skills/laravel/laravel-testing.md

Related Laravel Skills

Other Claude Code skills in the same category — free to download.

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.