Here is a complete beginner-friendly, step-by-step guide on:
Download : How to Set Up PHPUnit in Laravel (Full Explanation).mp4
Testing is one of the most important parts of professional Laravel development. Laravel comes with PHPUnit pre-installed, but many beginners don’t know how to configure and use it properly.
This guide will explain everything from scratch.
๐ฐ What is PHPUnit?
PHPUnit is a testing framework for PHP applications.
It allows you to:
Test your application automatically
Verify that your code works as expected
Prevent bugs when making changes
Build reliable and stable applications
Laravel uses PHPUnit as its default testing tool.
✅ Step 1 – Install Laravel (If Not Installed)
Before setting up PHPUnit, you need a Laravel project.
Run this command to create a fresh Laravel project:
composer create-project laravel/laravel laravel-test-app
Then go inside the project folder:
cd laravel-test-app
✅ Step 2 – Verify PHPUnit is Already Installed
Laravel automatically installs PHPUnit when you create a project.
To confirm, check the composer.json file.
Open it and find:
"require-dev": {
"phpunit/phpunit": "^10.0"
}
This means PHPUnit is already included!
✅ Step 3 – Understand the PHPUnit Configuration File
Laravel provides a pre-configured file named:
๐ phpunit.xml
This file controls how tests run.
Open phpunit.xml in your project root.
You will see something like:
<phpunit bootstrap="vendor/autoload.php"
colors="true">
This file defines:
Test directories
Environment variables
Database configuration for testing
✅ Step 4 – Testing Directory Structure
Laravel already creates a folder for tests:
๐ tests/
Inside it, there are two folders:
| Folder | Purpose |
|---|---|
| Feature | For testing application features |
| Unit | For testing small pieces of code |
Example:
tests/
├── Feature/
├── Unit/
├── TestCase.php
✅ Step 5 – Running PHPUnit for the First Time
To run PHPUnit tests, simply execute:
php artisan test
OR
vendor/bin/phpunit
If everything is correct, you will see:
✔ All tests passed
This means PHPUnit is successfully set up.
✅ Step 6 – Creating Your First Test
Let’s create a sample test.
Run this command:
php artisan make:test ExampleTest
This creates:
๐ tests/Feature/ExampleTest.php
Open that file:
public function test_example()
{
$response = $this->get('/');
$response->assertStatus(200);
}
This test checks:
๐ “Is the homepage loading correctly?”
Run the Test
php artisan test
If your website home page works, the test will pass.
✅ Step 7 – Setting Up Testing Database
Testing should NOT use your real database.
Laravel allows a separate testing database.
Open .env file and create a testing database:
DB_DATABASE=laravel
Now open:
๐ phpunit.xml
Find this section:
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
This means:
✔ Laravel will use an in-memory database for tests
✔ Your real data will stay safe
✅ Step 8 – Writing Unit Tests
Unit tests check small parts of code.
Create a unit test:
php artisan make:test MathTest --unit
Open:
๐ tests/Unit/MathTest.php
Example test:
public function test_addition()
{
$sum = 2 + 2;
$this->assertEquals(4, $sum);
}
Run again:
php artisan test
✅ Step 9 – Testing Controllers
Example:
Create a controller test:
php artisan make:test UserControllerTest
Inside the test:
public function test_user_page()
{
$response = $this->get('/users');
$response->assertStatus(200);
}
This ensures:
✔ Your routes and controllers work correctly
✅ Step 10 – Testing Models
You can also test database models.
Example:
public function test_user_creation()
{
$user = User::create([
'name' => 'Test',
'email' => 'test@example.com',
'password' => bcrypt('password')
]);
$this->assertDatabaseHas('users', [
'email' => 'test@example.com'
]);
}
๐ฏ Why PHPUnit is Important in Laravel
Using PHPUnit helps you:
Catch bugs early
Improve code quality
Automate testing
Save development time
Build professional applications
๐ Common PHPUnit Commands
| Command | Purpose |
|---|---|
| php artisan test | Run all tests |
| vendor/bin/phpunit | Run PHPUnit manually |
| php artisan make:test TestName | Create new test |
| php artisan make:test TestName --unit | Create unit test |
๐ Final Conclusion
Setting up PHPUnit in Laravel is very easy because:
✔ Laravel includes PHPUnit by default
✔ No extra installation needed
✔ Ready-to-use configuration
✔ Powerful testing features
By learning PHPUnit, you become a better Laravel developer.
Enjoy! Follow us for more...

































