Backend Development 7 min read

Introduction to Unit Testing with PHPUnit for PHP

This article explains the concept and importance of software testing, outlines various test types, and provides a step‑by‑step guide with code examples on installing PHPUnit, creating a Calculator class, writing unit tests, and running them to ensure reliable PHP applications.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Introduction to Unit Testing with PHPUnit for PHP

In software development, testing is essential for ensuring code works as expected and delivering high‑quality software. For PHP developers, unit testing is a key practice, and PHPUnit is the most popular testing framework.

What is Testing?

Testing evaluates a software system or its components to verify that they meet functional and performance requirements, helping identify errors early and guaranteeing that applications run as intended.

Test Types

Common testing types include:

Unit testing: testing the smallest testable units such as functions or methods.

Integration testing: testing interactions between multiple modules or components.

Functional testing: verifying that each feature works according to specifications.

End‑to‑end testing: simulating real user scenarios from start to finish.

Why Perform Unit Tests?

Unit tests provide early error detection, confidence when refactoring, serve as documentation of expected behavior, and encourage modular, testable code design.

Getting Started with PHPUnit

Installation

Install PHPUnit via Composer:

composer require --dev phpunit/phpunit

Step 1: Create a PHP Class

Create Calculator.php with basic arithmetic methods:

<?php

class Calculator {
    public function add($a, $b) { return $a + $b; }
    public function subtract($a, $b) { return $a - $b; }
    public function multiply($a, $b) { return $a * $b; }
    public function divide($a, $b) {
        if ($b === 0) { throw new InvalidArgumentException("Division by zero."); }
        return $a / $b;
    }
}

Step 2: Write Unit Tests

Create CalculatorTest.php in the test directory:

<?php

use PHPUnit\Framework\TestCase;
require 'Calculator.php';

class CalculatorTest extends TestCase {
    protected $calculator;
    protected function setUp(): void { $this->calculator = new Calculator(); }
    public function testAdd() {
        $this->assertEquals(5, $this->calculator->add(2, 3));
        $this->assertEquals(-1, $this->calculator->add(2, -3));
    }
    public function testSubtract() {
        $this->assertEquals(-1, $this->calculator->subtract(2, 3));
        $this->assertEquals(5, $this->calculator->subtract(2, -3));
    }
    public function testMultiply() {
        $this->assertEquals(6, $this->calculator->multiply(2, 3));
        $this->assertEquals(-6, $this->calculator->multiply(2, -3));
    }
    public function testDivide() {
        $this->assertEquals(2, $this->calculator->divide(6, 3));
        $this->expectException(InvalidArgumentException::class);
        $this->calculator->divide(1, 0);
    }
}

Test Explanation

The setUp() method initializes a Calculator object before each test. Individual test methods verify addition, subtraction, multiplication, and division, including exception handling for division by zero.

Step 3: Run the Tests

Execute the tests with:

./vendor/bin/phpunit tests/CalculatorTest.php

If everything is set up correctly, the output will indicate that all tests have passed.

Conclusion

Unit testing is a powerful practice that significantly improves the quality and reliability of PHP applications. Using PHPUnit, developers can detect bugs early, document expected behavior, and design more maintainable code, while advanced features like data providers and mock objects further enhance test suites.

Testingbackend developmentunit testingphpunit
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.