Scaffolding of unit tests with PHPUnit.

Author
By Darío Rivera
Posted On in PHPUnit

Adding tests to your software is no longer just a curiosity for those geek programmers. Today, it is increasingly necessary to implement automated tests and that they are linked to continuous integration implementations. Today, we will see how you can add a scaffolding of unit tests with PHPUnit in your PHP projects just as it is done in a professional environment. 

Requirements

To be able to develop this tutorial, you need:

- To have your project configured with composer. That is, to have a composer.json file and be able to install packages using composer.
- Have the code of your project configured in the composer.json autoloader file.

If you do not yet have your project configured with composer, I invite you to take what is necessary from this tutorial Create an installable PHP library with composer.

Configuration of the tests folder

The first step is to create some folder such as tests, and add the respective autoloader in the composer.json file.

"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/"
    }
}

Note that the autoloader will only be configured in your development environment. This prevents all the testing classes you do from loading in production.

Installation of PHPUnit

The next thing you need to do is install the latest version of PHPUnit.

composer require phpunit/phpunit --dev

Likewise, as in the autoloader configuration, this library will be installed only in the development environment. This is why it must be installed with the --dev argument.

Create the project's testing configuration

Once this is done, you can generate the initial testing configuration for your project with the following command.

vendor/bin/phpunit --generate-configuration

You will get an output similar to the following. If you have done everything right up to this point, it will be a matter of taking the default values and pressing enter to the phpunit questions.

PHPUnit 9.3.8 by Sebastian Bergmann and contributors.

Generating phpunit.xml in /var/www/...

Bootstrap script (relative to path shown above; default: vendor/autoload.php):
Tests directory (relative to path shown above; default: tests):
Source directory (relative to path shown above; default: src):

Generated phpunit.xml in /var/www/...

This command will generate a file called phpunit.xml that contains the configuration for phpunit. You will surely want to avoid headaches and change some values in this file. Personally, I change the following attributes to false in the phpunit tag.

forceCoversAnnotation="false"
beStrictAboutCoversAnnotation="false"

In addition to this, if you want to see your tests execution a little better, I recommend placing the following attribute in the same tag.

colors="true"

Creating your first unit test

Once this is done, you can create your first unit test. Just for testing purposes, an obvious assertion has been created. The idea is that since you already have your project autoloaded, you can test your software components.

namespace Tests;

use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
    /**
     * @test
     */
    public function itCanTestSomething()
    {
        $this->assertTrue(true);
    }
}

Running the tests

To run the tests in the class created above, simply run the following command.

vendor/bin/phpunit --configuration "phpunit.xml" --filter=ExampleTest

If everything has gone well, you should get an output similar to the following.

PHPUnit 9.3.8 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.4.3
Configuration: phpunit.xml

.                               1 / 1 (100%)

Time: 00:00.011, Memory: 6.00 MB

OK (1 test, 1 assertion)

Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.