How to Implement Testing in Laravel — A Simple Guide for Developers
Testing is very important when you build a Laravel application. It helps make sure your code is working fine and nothing is broken when you change something. Laravel makes testing easy with built-in tools like PHPUnit and Pest.
In this post, I will explain how to use Laravel testing step by step. I will keep it simple and clear for non-native English speakers.
What Is Testing in Laravel
Testing is a way to check if your code is doing what you expect. Laravel gives you a testing setup with PHPUnit by default.
There are two common types of tests in Laravel:
Feature Tests – test how your app works as a whole, like clicking buttons or submitting forms
Unit Tests – test small parts of your code like a method or a function
You can also use Pest – a simple and clean testing tool that works with Laravel.
Step 1 – Setup Laravel for Testing
If you created your project with laravel new, you already have PHPUnit installed.
Check the phpunit.xml file in your Laravel root folder. This is the main config file for running tests.
Install Pest (Optional)
If you like a cleaner syntax, you can install Pest like this:
composer require pestphp/pest --dev
php artisan pest:install
Now you can use Pest for writing tests too.
Step 2 – Create a Test
Use this Artisan command to create a new test:
php artisan make:test ExampleTest
Or, for a unit test:
php artisan make:test ExampleTest --unit
Laravel will create the test file in the tests/Feature or tests/Unit folder.
Step 3 – Write a Simple Test
Here is a basic feature test example:
public function test_homepage_is_working()
{
$response = $this->get('/');
$response->assertStatus(200);
}
This test checks that your homepage is loading correctly and gives a status code 200.
Step 4 – Run the Tests
To run all your tests, just use this command:
php artisan test
Or if you are using Pest:
./vendor/bin/pest
You will see the test results in your terminal.
Step 5 – Test With Database
Laravel makes it easy to use the database in your tests.
Use this in your test class:
use Illuminate\Foundation\Testing\RefreshDatabase;
Then in the class:
class UserTest extends TestCase
{
use RefreshDatabase;
public function test_user_creation()
{
$user = User::factory()->create();
$this->assertDatabaseHas('users', [
'email' => $user->email,
]);
}
}
This creates a user using a factory and checks if the user is saved in the database.
Step 6 – Use Factories
Factories help you create fake data for testing. To make one:
php artisan make:factory UserFactory --model=User
Then use it like this in your test:
$user = User::factory()->create();
Factories are very useful for testing real-world cases.
Step 7 – Test Auth and API
Here is how you can test routes that require login:
public function test_logged_in_user_can_access_dashboard()
{
$user = User::factory()->create();
$response = $this->actingAs($user)->get('/dashboard');
$response->assertStatus(200);
}
And to test API routes:
public function test_api_returns_json()
{
$response = $this->getJson('/api/posts');
$response->assertStatus(200)
->assertJsonStructure(['data']);
}
Bonus – Useful Assertions
Here are some common ones:
$this->assertTrue(condition) – check if something is true
$this->assertEquals(a, b) – check if two values are equal
$response->assertRedirect('/home') – check if redirect happened
$response->assertSee('Welcome') – check if text is on page
Conclusion
Testing may look boring at first, but it saves your time and makes your app better and safer. Laravel gives you a strong testing setup by default. Start small, test important features, and grow from there.
Even if you are a non-native English speaker, testing is something you can learn step by step.
If you want, I can also turn this into a PDF or share a simple GitHub repo with sample test files. Just let me know.