As of: 4 May 2026 · Reading time: 4 min
Key takeaways
- Write robust tests for your Laravel application!
- Our guide will guide you through unit tests, feature tests, database testing and test drives development.
Write robust tests for your Laravel application! Our guide will guide you through unit tests, feature tests, database testing and test drives development.
“Laravel is our framework of choice for complex web applications—fast, secure, and maintainable.”
– Björn Groenewold, Managing Director, Groenewold IT Solutions
Why Test Coverage Matters
Short: High test coverage reduces regressions.
High test coverage reduces regressions. It enables safe refactoring. It gives the development team confidence to make changes without fear of breaking existing functionality.
Laravel provides a complete testing ecosystem built on PHPUnit and the Pest framework. Both are supported out of the box.
Three Types of Tests in Laravel
Unit Tests
Unit tests verify individual classes and methods in isolation. No database, no HTTP requests, no external services.
Location: tests/Unit/
Use unit tests for: business logic classes, data transformation functions, validation rules, calculation helpers.
Feature Tests
Feature tests verify HTTP request-response cycles and database interactions. They make actual requests to your application routes and assert the results.
Location: tests/Feature/
Use feature tests for: registration and login flows, API endpoints, form submissions, authorization checks.
Browser Tests (Dusk)
Browser tests run the application in a real browser using ChromeDriver. They test JavaScript-heavy interactions that feature tests cannot cover.
Location: tests/Browser/
Use browser tests for: complex frontend interactions, JavaScript-dependent workflows, end-to-end user journeys.
Running Tests
Short: # Run all tests php artisan test # Run only unit tests php artisan test --testsuite=Unit # Run only feature tests php artisan test --testsuite=Feature # Run a specific test class php artisan test --filter=UserRegistrationTest # Run in parallel for faster execution php artisan test --parallel Your First Feature Test php artisan make:test UserRegistrationTest <?
# Run all tests php artisan test # Run only unit tests php artisan test --testsuite=Unit # Run only feature tests php artisan test --testsuite=Feature # Run a specific test class php artisan test --filter=UserRegistrationTest # Run in parallel for faster execution php artisan test --parallel Your First Feature Test php artisan make:test UserRegistrationTest <?php namespace Tests\Feature; use Tests\TestCase; use Illuminate\Foundation\Testing\RefreshDatabase; class UserRegistrationTest extends TestCase { use RefreshDatabase; public function test_registration_page_renders(): void { $response = $this->get('/register'); $response->assertStatus(200); } public function test_new_user_can_register(): void { $response = $this->post('/register', [ 'name' => 'Test User', 'email' => 'test@example.com', 'password' => 'password', 'password_confirmation' => 'password', ]); $this->assertAuthenticated(); $response->assertRedirect('/dashboard'); } } Database Testing With RefreshDatabase The RefreshDatabase trait resets the database after each test.This ensures tests do not interfere with each other.
use Illuminate\Foundation\Testing\RefreshDatabase; class UserTest extends TestCase { use RefreshDatabase; public function test_user_can_be_created(): void { $user = User::factory()->create([ 'name' => 'Test User', 'email' => 'test@example.com', ]); $this->assertDatabaseHas('users', [ 'email' => 'test@example.com', ]); } } Model Factories for Test Data Factories generate realistic test data automatically.Define them once; use them across all tests.
// database/factories/UserFactory.php
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => Hash::make('password'),
];
}
// Add states for specific scenarios
public function admin(): static
{
return $this->state(fn (array $attributes) => [
'role' => 'admin',
]);
}
// In your test
$user = User::factory()->create();
$admin = User::factory()->admin()->create();
$users = User::factory()->count(10)->create();
HTTP Assertions for Feature Tests
Short: Laravel provides a rich set of assertion methods for HTTP testing:
Laravel provides a rich set of assertion methods for HTTP testing:
// Status code assertions $response->assertStatus(200); $response->assertOk(); $response->assertCreated(); $response->assertNotFound(); $response->assertForbidden(); // Redirect assertions $response->assertRedirect('/expected-url'); // Response content assertions $response->assertJson(['key' => 'value']); $response->assertJsonPath('data.email', 'test@example.com'); $response->assertSee('Welcome'); // Database assertions $this->assertDatabaseHas('users', ['email' => 'test@example.com']); $this->assertDatabaseMissing('users', ['email' => 'deleted@example.com']); Continuous Integration Run tests automatically on every code push.Configure your CI system (GitHub Actions, GitLab CI) to execute the test suite before merging pull requests.
# .github/workflows/tests.yml
- name: Run tests
run: php artisan test --parallel
Failed tests block the merge. This prevents regressions from reaching production.
What Good Test Coverage Achieves
- Regressions are caught automatically — not by users
- Refactoring becomes safe — tests verify behavior is preserved
- Onboarding is faster — tests document how the system is expected to behave
- Deployment confidence increases — the team knows what has been verified
"Laravel offers a comprehensive test ecosystem with PHPUnit and Pest — unit tests for isolated logic, feature tests for HTTP requests and database interactions." — Björn Groenewold, Managing Director, Groenewold IT Solutions
About the author
Managing Director of Groenewold IT Solutions GmbH and Hyperspace GmbH
Since 2009 Björn Groenewold has been developing software solutions for the mid-market. He is Managing Director of Groenewold IT Solutions GmbH (founded 2012) and Hyperspace GmbH. As founder of Groenewold IT Solutions he has successfully supported more than 250 projects – from legacy modernisation to AI integration.
Blog recommendations
Related articles
These posts might also interest you.

Laravel Deployment: From Local to Production 2026
Bring your Laravel application to production! Our guide will guide you through server setup, deployment strategies, optimization and best practices.

Laravel 12: All new features and improvements 2026
Discover all the new features of Laravel 12! Our comprehensive guide presents the most important innovations, improvements and breaking changes.

Laravel Tutorial German: The ultimate guide for...
Learn Laravel from scratch! Our comprehensive German tutorial for beginners will guide you through the installation, the MVC concept and your first project. Start your trip to Laravel-Profi now.
Free download
Checklist: 10 questions before software development
Key points before you start: budget, timeline, and requirements.
Get the checklist in a consultationRelevant next steps
Related services & solutions
Based on this article's topic, these pages are often the most useful next steps.
Related services
Related solutions
Cost calculators
More on Laravel and next steps
This article is in the Laravel topic. In our blog overview you will find all articles; under category Laravel more posts on this subject.
For topics like Laravel we offer matching services – from app development and AI integration to legacy modernisation and maintenance. We describe typical use cases under solutions. Our cost calculators give initial estimates. Key terms are in the IT glossary. Books and long-form guides appear on the publications page; deeper articles live under topics.
If you have questions about this article or want a non-binding discussion about your project, you can book a consultation or reach us via contact. We usually respond within one working day.

