Laravel is built with testing in mind. It ships with PHPUnit configured and provides a rich set of helpers to test HTTP, database, mail, queues, and more.
// Feature tests — test full HTTP requests and interactions
tests/Feature/
// Unit tests — test isolated PHP classes and methods
tests/Unit/
// Create tests
php artisan make:test PostControllerTest // Feature (default)
php artisan make:test PostServiceTest --unit // Unit
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PostControllerTest extends TestCase
{
use RefreshDatabase;
public function test_authenticated_user_can_view_posts(): void
{
$user = User::factory()->create();
Post::factory(5)->for($user)->create();
$response = $this->actingAs($user)->get(route('posts.index'));
$response->assertStatus(200);
$response->assertViewHas('posts');
}
}
php artisan test // Run all tests
php artisan test --filter=PostControllerTest // Run a single class
php artisan test --filter=test_user_can_view // Run a single method
php artisan test --parallel // Run in parallel (faster)
// RefreshDatabase wraps each test in a transaction and rolls back after.
// Use it in any test that touches the database — it keeps tests isolated
// and fast without dropping/recreating tables.
use Illuminate\Foundation\Testing\RefreshDatabase;
class ExampleTest extends TestCase
{
use RefreshDatabase;
}