Writing Unit and Feature Tests

Tutorial 2 of 5

1. Introduction

Welcome to this tutorial! Our goal is to learn how to write unit and feature tests for a Laravel application. Testing is critical as it ensures your code behaves as expected and makes it easier to make changes without breaking existing functionality.

By the end of this tutorial, you will:
- Understand the basics of unit and feature tests
- Know how to write and run tests in Laravel
- Be familiar with best practices for writing tests

Prerequisites:
- Basic understanding of PHP
- Familiarity with the Laravel framework
- A Laravel application to work with

2. Step-by-Step Guide

In Laravel, testing is facilitated by the PHPUnit testing framework. Unit tests focus on small, isolated parts of the application's codebase, like individual methods or functions. Feature tests, on the other hand, test a larger portion of your code, often including how several units interact together.

Writing a Basic Test

In Laravel, all tests are stored in the tests directory. Unit tests should be stored in the tests/Unit directory and feature tests in the tests/Feature directory.

Here's an example of a basic unit test:

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $this->assertTrue(true);
    }
}

In this example, we have a single test method testBasicTest() that asserts that true is true.

Running Tests

You can run your tests using the php artisan test command in your terminal. The output will show you which tests passed and which failed.

3. Code Examples

Let's write a unit test for a function that adds two numbers:

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;
use App\Calculator;

class CalculatorTest extends TestCase
{
    /**
     * Test the add method.
     *
     * @return void
     */
    public function testAdd()
    {
        $calculator = new Calculator;

        $result = $calculator->add(1, 2);

        $this->assertEquals(3, $result);
    }
}

Here, we're testing the add method of the Calculator class. We create a new instance of Calculator, call the add method with the parameters 1 and 2, and then assert that the result is 3.

4. Summary

We've covered the basics of writing and running unit and feature tests in Laravel. We've learned that unit tests are for testing small, isolated parts of the codebase, while feature tests are for testing larger portions of the code. Testing is an essential part of the development process, and Laravel makes it easy with the PHPUnit testing framework.

5. Practice Exercises

  1. Write a unit test for a function that multiplies two numbers.
  2. Write a feature test for a user registration process. This should include filling out a form and checking if the user was saved in the database.

Remember, practice is key when it comes to mastering testing. Keep exploring, keep learning and keep testing. Happy coding!