Learn PHPUnit: Module 1 Overview of Testing Fundamentals


Welcome to the first module of our PHPUnit course! Here, we'll lay the foundations for your journey into the world of automated testing. By the end of this module, you'll have a clear understanding of what tests are, why they're crucial for developing quality software and how to take your first steps with PHPUnit
1. What are Automated Tests?
Imagine you're building a house. Before handing it over, you check that the windows open and close, that the taps don't leak and that the electricity works in every room. These are manual tests. In software development, we do something similar.
Manual software testing involves one person running the program and checking that everything works as expected. Although important, this process can be:
Slow and repetitive: Running the same tests with every new change to the code is tiring.
Prone to human error: It's easy to forget to test a specific scenario or make a mistake during verification.
Not scalable: As the software grows, the number of tests required increases exponentially
This is where automated testing comes in. Instead of a person, we write code to test our own code. These tests are run by a program, which checks that the behavior of the software is as expected.
Types of Automated Tests:
There are several types of tests, each with a different focus:
Unit testing: They are the focus of this course. They test the smallest single unit of code (a function, a method or a class). The aim is to ensure that each “piece” of your software works correctly.
Integration Testing: Checks that different units of code work well together. For example, if your “User” class interacts correctly with the “Database” class.
Functional (or End-to-End - E2E) Testing: Simulates the behavior of a real user, testing complete application flows. For example, a functional test could simulate a user logging in, adding a product to the cart and finalizing the purchase.
In this course, our main focus will be on unit tests, as they form the basis of a solid testing strategy and are the ideal starting point for those just starting out.
2. Introducing PHPUnit
PHPUnit is the most popular and widely used testing framework in the PHP ecosystem. It provides a set of tools and a structure for writing and executing tests in an organized and efficient way.
Why PHPUnit?
Community Standard: It is the de facto standard for PHP testing, which means that there is a vast amount of documentation, tutorials and community support available.
Feature-rich: It offers a wide range of features for writing tests, from simple assertions to the creation of “test doubles” (mocks and stubs), which we'll look at later.
Easy integration: Integrates perfectly with popular development tools, such as Composer, and with Continuous Integration (CI/CD) systems.
Detailed Reports: Generates clear reports on test execution, indicating which tests passed, which failed and why.
3. Setting up the environment
To start using PHPUnit, you'll need to have it installed in your project. The recommended way to do this is via Composer, PHP's dependency manager. If you don't have Composer installed yet, you can find the instructions on the official website.
With Composer installed, open the terminal in your project folder and run the following command:
composer require --dev phpunit/phpunit
Let's understand what this command does:
composer require
: Tells Composer that you want to add a new dependency to your project.--dev
: This is an important flag. It indicates that PHPUnit is a development dependency, i.e. it is needed to develop and test the application, but does not need to be installed in a production environment.phpunit/phpunit
: This is the name of the PHPUnit package in the Packagist (Composer's package repository).
4. Your First Test
Let's create a simple test to see PHPUnit in action. The most common convention is to have a src
(or app
) folder for your application code and a tests
folder for the tests, mirroring the src
structure.
my-project/
├── src/
│ └── Example.php
├── tests/
│ └── ExampleTest.php
├── vendor/
└── composer.json
1. Class to be tested
Inside the src/
folder, create a file named Example.php
.
<?php
namespace App;
class Example
{
public function sum(int $a, int $b): int
{
return $a + $b;
}
}
2. Create a test file
Inside the tests/
folder, create a file named ExampleTest.php
. The convention is to name test files with the Test.php
suffix.
<?php
use PHPUnit\Framework\TestCase;
use App\Example;
class ExampleTest extends TestCase
{
public function testSum()
{
$calc = new Example();
$result = $calc->sum(2, 3);
$this->assertEquals(5, $result);
}
}
Let's analyze this code:
use PHPUnit\Framework\TestCase;
: Imports the TestCase class from PHPUnit, which all of our test classes must extend.class ExampleTest extends TestCase
: Defines our test class, which extendsTestCase
.public function testSum()
: This is our test method. The convention is for test method names to start with thetest
prefix.$result = $calc->add(2, 3);
: The code we want to test.$this->assertEquals(5, $result);
: This is an assertion. We are telling PHPUnit: "I assert that the expected value (5) is equal to the actual value ($result
)". If the assertion is true, the test passes. If it's false, the test fails.
3. Configuring autoload (composer.json
)
In your composer.json
file, add the following:
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
}
Then run:
composer dump-autoload
4. Run the test
Now, in your terminal, from the root of your project, run the following command:
./vendor/bin/phpunit tests
./vendor/bin/phpunit
: Executes the PHPUnit executable that was installed by Composer.tests
: Tells PHPUnit to look for and run the tests in thetests/
folder.
If everything went well, you should see output similar to this:
PHPUnit x.x.x by Sebastian Bergmann and contributors.
Runtime: PHP x.x.x
. 1 / 1 (100%)
Time: 00:00.024, Memory: 14.00 MB
OK (1 test, 1 assertion)
The dot (.
) indicates that a test passed successfully. If the test had failed (for example, if we expected the result to be 5 but it wasn't), the output would be different, showing an F
and a detailed error message.
Congratulations! You have just written and executed your first test with PHPUnit. In the next module, we will dive deeper into writing tests and explore the various assertions that PHPUnit offers.
Module 1 Final Challenge
Create a subtract($a, $b)
method in the Example
class and write a test for it in ExampleTest
.
Subscribe to my newsletter
Read articles from Antonio Silva directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Antonio Silva
Antonio Silva
Systems Development Technician and PHP Developer.