Home > Backend Development > PHP Tutorial > Build Bug-Free Applications using Laravel Unit Testing

Build Bug-Free Applications using Laravel Unit Testing

Linda Hamilton
Release: 2025-01-03 05:43:40
Original
433 people have browsed it

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
Copy after login
Copy after login
Copy after login
Copy after login

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
Copy after login
Copy after login

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);
    }
}
Copy after login
Copy after login

3. Run the Test

Run your test using:

php artisan test
Copy after login
Copy after login
Copy after login
Copy after login

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 make:test SumOfTwoNumberTest --unit
Copy after login
Copy after login

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:

<?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);
    }
}
Copy after login
Copy after login

2. Create the Test

Generate a test file:

./vendor/bin/phpunit
Copy after login

Edit the test file to test the getFullName method:

php artisan test
Copy after login
Copy after login
Copy after login
Copy after login

3. Execute the Test

Run the test:

public function getFullName(): string
{
    return $this->first_name .' '. $this->last_name;
}
Copy after login

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 UserTest --unit
Copy after login

This will create tests/Feature/LoginTest.php.

2. Write the Test Logic

Open LoginTest.php file and add the below code:

<?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());
    }
}
Copy after login

3. Run the Feature Test

php artisan test
Copy after login
Copy after login
Copy after login
Copy after login

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 behavior. With PHPUnit writing and running tests is a straightforward and efficient way. By integrating tests into your development process, you’ll create more reliable, maintainable applications.

Happy Reading!!
Happy Coding!!
❤️ ?

The above is the detailed content of Build Bug-Free Applications using Laravel Unit Testing. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template