Lesson 42 of 50
Automated testing means writing code that checks whether your application behaves as expected. Laravel provides testing tools that help developers detect problems before deploying an application.
Why is testing important?
Common types of tests:
Laravel projects commonly use PHPUnit or Pest for automated tests. New projects may include a testing framework configured by default.
Laravel stores automated tests in the
tests directory.
tests/
├── Feature/
│ └── ExampleTest.php
└── Unit/
└── ExampleTest.php
Feature directory:
Use feature tests to check application behavior, including routes, controllers, validation, and database operations.
Unit directory:
Use unit tests for small pieces of logic that can be tested independently.
The exact files and configuration can differ depending on the Laravel version and whether your project uses PHPUnit or Pest.
Use Artisan to create a feature test:
php artisan make:test StudentTest
Laravel creates a test file inside
tests/Feature.
Example using PHPUnit:
<?php
namespace Tests\Feature;
use Tests\TestCase;
class StudentTest extends TestCase
{
public function test_home_page_returns_successful_response(): void
{
$response = $this->get('/');
$response->assertStatus(200);
}
}
Explanation:
$this->get('/') sends a simulated HTTP GET request.assertStatus(200) checks that the response has status code 200.This example assumes your application has a home route that returns a successful response.
Run all tests with:
php artisan test
You can also run tests using PHPUnit directly if it is installed and configured in your project:
vendor/bin/phpunit
To run one test file, use:
php artisan test tests/Feature/StudentTest.php
Typical results:
Read the failure message and stack trace to identify which assertion or operation needs attention.
Laravel feature tests can simulate HTTP requests to your application's API routes.
Create a test for an endpoint that returns a list of students:
<?php
namespace Tests\Feature;
use Tests\TestCase;
class StudentApiTest extends TestCase
{
public function test_students_api_returns_successful_response(): void
{
$response = $this->getJson('/api/students');
$response->assertOk();
$response->assertJsonStructure([
'data',
]);
}
}
This example assumes your
/api/students endpoint returns a JSON
response containing a top-level data key.
If your endpoint returns a different JSON structure, update the assertions to match its actual response.
Useful response assertions:
$response->assertOk();
$response->assertCreated();
$response->assertNotFound();
$response->assertUnauthorized();
$response->assertJson(['message' => 'Success']);
Laravel provides testing helpers for checking database
operations. The RefreshDatabase trait
is commonly used to prepare a clean database state
for tests.
Example test for creating a student:
<?php
namespace Tests\Feature;
use App\Models\Student;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class StudentCreateTest extends TestCase
{
use RefreshDatabase;
public function test_student_can_be_created(): void
{
$response = $this->postJson('/api/students', [
'name' => 'Rahul Kumar',
'email' => 'rahul@example.com',
'course' => 'Laravel',
]);
$response->assertCreated();
$this->assertDatabaseHas('students', [
'email' => 'rahul@example.com',
'name' => 'Rahul Kumar',
]);
}
}
This test assumes your API accepts these fields,
returns HTTP 201 after creation, and uses the
students table.
Configure a dedicated testing database or another safe test database setup before running tests that reset database tables. Never run destructive test migrations against production data.
Automated tests can verify that Laravel rejects invalid request data.
Example test:
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class StudentValidationTest extends TestCase
{
use RefreshDatabase;
public function test_student_requires_valid_input(): void
{
$response = $this->postJson('/api/students', [
'name' => '',
'email' => 'invalid-email',
'course' => '',
]);
$response->assertUnprocessable();
$response->assertJsonValidationErrors([
'name',
'email',
'course',
]);
}
}
The test sends invalid data and expects HTTP 422, which is commonly used for JSON validation errors.
The test assumes the API requires all three fields and uses Laravel's standard validation response.
Testing invalid input helps ensure that incomplete or incorrect data does not enter your database.
Important:
A passing test means the tested conditions succeeded. It does not guarantee that every possible application behavior is correct. Write tests that cover the most important risks and business rules.
Build a small automated test suite for your Student API.
php artisan test.Bonus Task:
Add authentication to your API and write a test that verifies an unauthenticated user cannot access a protected student endpoint.
1. Which command runs Laravel's automated tests?
2. Which trait is commonly used to reset the database during tests?
3. What does a feature test commonly test?