Master unit testing in Laravel with practical examples

Diego Cortés
Diego Cortés
January 21, 2025
Master unit testing in Laravel with practical examples

Laravel is one of the most popular PHP frameworks in web development, and its ability to perform unit testing is one of its standout features. A good set of unit tests not only ensures that your application functions as expected, but also allows you to identify bugs early. Below, we will explore how to master unit testing in Laravel through practical examples.

What are unit tests?

Unit tests are a software development technique in which the functioning of the smallest units of code, such as functions or methods, is verified in isolation. In Laravel, this process is straightforward thanks to its powerful testing system.

Setting up the testing environment

Before starting with unit tests, it's essential to set up the testing environment in Laravel. Laravel uses PHPUnit as its default testing framework. To ensure everything is ready, you need to install PHPUnit and have the correct configuration in your phpunit.xml file.

composer require --dev phpunit/phpunit

Additionally, it is good practice to have a test database to avoid modifying production data.

Creating your first unit test

To illustrate the process, let's consider a simple example of a function that adds two numbers. First, we will create the class that contains this method.

namespace App\Calculator;

class Addition
{
    public function add($a, $b)
    {
        return $a + $b;
    }
}

Next, we create a unit test for this class. Run the following command to generate a test:

php artisan make:test AdditionTest

This will create a new test file in the tests/Feature directory. Open this file and add the following code:

use Tests\TestCase;
use App\Calculator\Addition;

class AdditionTest extends TestCase
{
    public function test_add()
    {
        $addition = new Addition();
        $result = $addition->add(2, 3);
        $this->assertEquals(5, $result);
    }
}

With this setup, we are testing whether the add method returns the correct sum.

Running tests

To run the tests, simply use the command:

php artisan test

This will execute all tests in your application and provide a summary of the results.

Tests for the Eloquent model

In addition to simple functions, it’s crucial to conduct tests on Eloquent models. Imagine you have a model named Product. You can perform tests to ensure that the model interacts correctly with the database.

First, create a new test using the same command as before:

php artisan make:test ProductTest

In this case, you might check if a product can be created correctly in the database:

use Tests\TestCase;
use App\Models\Product;

class ProductTest extends TestCase
{
    public function test_create_product()
    {
        $product = Product::create([
            'name' => 'Product 1',
            'price' => 100,
        ]);

        $this->assertDatabaseHas('products', [
            'name' => 'Product 1',
        ]);
    }
}

Unit tests help ensure that any future changes in your application do not affect its correct functionality.

Conclusion

Unit tests are a fundamental part of development in Laravel. This practice not only improves code quality but also optimizes the development process by facilitating bug detection. I encourage you to implement these tests in your projects; the time investment in this technique will be reflected in the stability and functionality of your applications.

If you’re interested in learning more about Laravel and related topics, I invite you to keep reading similar news on my blog.

Article information

Published: January 21, 2025
Category: Laravel Tutorials
Reading time: 5-8 minutes
Difficulty: Intermediate

Key tips

1

Take your time to understand each concept before moving on to the next one.

2

Practice the examples in your own development environment for better understanding.

3

Don't hesitate to review the additional resources mentioned in the article.

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Frequently Asked Questions

Categories

Page loaded in 22.52 ms