DEV Community

Cover image for Build Bug-Free Applications using Laravel Unit Testing
Snehal Rajeev Moon
Snehal Rajeev Moon

Posted on

Build Bug-Free Applications using Laravel Unit Testing

Hello Artisan,

Testing is a part of software development. It ensures your application is working as expected. Laravel has good support for testing, and it was designed with testing in mind. It provides Pest and PHPUnit for testing.

Once you install any laravel application you can see phpunit.xml already available in your application. By default application contains two separate directories in test directory one is for Feature and another is Unit. Unit is a smaller and isolated part of your application while Feature is used to test a larger part of your applications.

In this blog post, we will explore how to write tests using PHPUnit and how to integrate with your laravel application.

Why Unit Testing Matters?

Before getting into the details, let's quickly get a sense of why unit testing is important:

  • Code Quality: Tests catch bugs early in the game, thereby not risking them entering production.

  • Refactoring Confidence: Tests allow you to refactor with confidence because you don't fear breaking functionality.
    Documentation: Tests act as living documentation for the behavior of your code.

  • Collaboration: They serve as a safety net for teams: they do not allow new changes to disrupt existing functionality.

Before we start writing the test cases, ensure you have installed the Laravel application.

Development environment is set up: Create a .env.testing file in the root directory of your project. This file is used instead of the .env file when you are running PHPUnit tests or executing Artisan commands with the --env=testing option.

Verify PHPUnit Configuration: Check the phpunit.xml file in your project root. This file configures _PHPUnit_ for your Laravel application.

Run the Default Tests: Laravel includes some example tests. You can run them using:

php artisan test
Enter fullscreen mode Exit fullscreen mode

This command executes all tests in the tests directory.

So let's start writing Your First Unit Test

Feature: For testing larger chunks of your application, often involving HTTP requests.

Unit: For testing individual classes and methods.

Let’s write a simple unit test:

1. Create a Test File

Use Artisan to generate a test file:

php artisan make:test SumOfTwoNumberTest --unit
Enter fullscreen mode Exit fullscreen mode

This will create tests/Unit/SumOfTwoNumberTest.php.

2. Write a Test Case

Open the newly created test file and add your test logic:

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;

class SumOfTwoNumberTest extends TestCase
{
    /**
     * A basic unit test example.
     *
     * @return void
     */
    public function test_addition()
    {
        $sum = 2 + 2;
        $this->assertEquals(4, $sum);
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Run the Test

Run your test using:

./vendor/bin/phpunit
Enter fullscreen mode Exit fullscreen mode

You can also use the test Artisan command to run your tests. This command provides verbose test reports to ease the development and debugging process.

php artisan test
Enter fullscreen mode Exit fullscreen mode

You should see an output indicating whether the test passed or failed.

  • Testing a Laravel Model

Let’s create a unit test for a Laravel model method. Assume we have a User model with a method getFullName:

1. Model Method

Add the method to your User model:

public function getFullName(): string
{
    return $this->first_name .' '. $this->last_name;
}
Enter fullscreen mode Exit fullscreen mode

2. Create the Test

Generate a test file:

php artisan make:test UserTest --unit
Enter fullscreen mode Exit fullscreen mode

Edit the test file to test the getFullName method:

<?php

namespace Tests\Unit;

use App\Models\User;
use PHPUnit\Framework\TestCase;

class UserTest extends TestCase
{
    public function test_get_full_name()
    {
        $user = new User();
        $user->first_name = 'John';
        $user->last_name = 'Doe';

        $this->assertEquals('John Doe', $user->getFullName());
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Execute the Test

Run the test:

php artisan test
Enter fullscreen mode Exit fullscreen mode

You should see a success message if the method works as expected.

  • Writing a Feature Test

Feature tests in Laravel allow you to test larger parts of your application, such as routes, controllers, and middleware.

Let’s write an example feature test for a login page.

1. Generate a Feature Test

Use Artisan to create a new feature test:

php artisan make:test LoginTest
Enter fullscreen mode Exit fullscreen mode

This will create tests/Feature/LoginTest.php.

2. Write the Test Logic

Open LoginTest.php file and add the below code:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class LoginTest extends TestCase
{
    use RefreshDatabase;

    /**
     * Test login page loads successfully.
     *
     * @return void
     */
    public function test_login_page_is_accessible()
    {
        $response = $this->get('/login');

        $response->assertStatus(200);
        $response->assertSee('Login');
    }

    /**
     * Test users can login with valid credentials.
     *
     * @return void
     */
    public function test_user_can_login_with_valid_credentials()
    {
        $user = User::factory()->create([
            'password' => bcrypt('password123'),
        ]);

        $response = $this->post('/login', [
            'email' => $user->email,
            'password' => 'password123',
        ]);

        $response->assertRedirect('/home');
        $this->assertAuthenticatedAs($user);
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Run the Feature Test

php artisan test
Enter fullscreen mode Exit fullscreen mode

The output will show whether the login page is accessible and if the user can log in successfully.

Some best practices for writing Unit Test in Laravel

  • Descriptive Test Names: The names of the tests should be very descriptive of what is being tested.

  • Test One Thing at a Time: Every test should test only one functionality or behavior.

  • Use Factories and Seeders: Make use of Laravel's model factories and seeders to create test data.

  • Isolate Tests: Unit tests should not depend on external services such as a database or an API. Use mocks and stubs when necessary.

You can check Laravel documentation for more details.

Conclusion:
Unit and feature testing in Laravel are powerful ways to maintain code quality and ensure robust application behaviour. With the help of Laravel's PHPUnit writing and running tests is straightforward and efficient. Integrating tests into your development process will create more reliable, maintainable applications.

Happy Reading!!
Happy Coding!!
❤️ 🦄

Top comments (0)