phpunit
Run PHP unit tests
TLDR
Run tests in the current directory. Note: Expects you to have a 'phpunit.xml'
Run tests in a specific file
Run tests annotated with the given group
Run tests and generate a coverage report in HTML
SYNOPSIS
phpunit
[options] [file|directory]
PARAMETERS
--configuration (-c) <file>
Reads configuration from an XML file, typically phpunit.xml.
--testsuite <name>
Only runs tests belonging to the specified test suite.
--filter <pattern>
Runs only tests whose names match the given pattern.
--bootstrap <file>
A PHP file that is included before the tests are run.
--verbose (-v)
Enables more detailed output during test execution.
--colors
Uses ANSI colors in the console output for better readability.
--stop-on-failure
Halts test execution immediately after the first test failure.
--coverage-html <dir>
Generates an HTML code coverage report in the specified directory.
--version
Displays the PHPUnit version information and exits.
--help
Displays a help message with command-line options and exits.
DESCRIPTION
PHPUnit is a programmer-oriented testing framework for the PHP language. It is a fundamental tool for developers practicing Test-Driven Development (TDD) or simply striving for robust and maintainable codebases.
At its core, PHPUnit allows developers to write repeatable tests for small, isolated units of code (functions, methods, classes). These tests ensure that components behave as expected, catching bugs early and preventing regressions when changes are introduced.
Tests are typically written as PHP classes extending PHPUnit\Framework\TestCase, with individual test methods prefixed with test. PHPUnit provides a rich set of assertions to verify outcomes, powerful test runners for execution, and comprehensive code coverage reporting tools to assess test effectiveness.
CAVEATS
PHPUnit requires a functional PHP installation to run. Modern projects typically manage it via Composer, requiring vendor/autoload.php to be set up correctly for test class discovery.
While powerful, its configuration can become complex for large projects. Code coverage generation can also be resource-intensive, particularly for extensive test suites or when dealing with large codebases.
CONFIGURATION FILE (<B>PHPUNIT.XML</B>)
PHPUnit can be extensively configured using an XML file, typically named phpunit.xml or phpunit.xml.dist. This file allows specifying test suites, bootstrapping files, code coverage settings, various PHP settings, and test listeners, providing a centralized way to manage test execution behavior without relying solely on command-line arguments.
TEST DISCOVERY
By default, PHPUnit scans specified directories for files ending with Test.php or methods prefixed with test within classes extending PHPUnit\Framework\TestCase. The configuration file can precisely define which directories or files to include for test discovery.
ASSERTIONS
Central to writing tests, assertions are methods like assertEquals(), assertTrue(), assertInstanceOf(), and many others, provided by the base PHPUnit\Framework\TestCase class. They are used to verify that a unit of code behaves as expected, failing the test if the condition is not met.
HISTORY
PHPUnit was originally created by Sebastian Bergmann, drawing inspiration from JUnit, the de facto standard testing framework for Java. Its initial public release dates back to 2004, quickly becoming the leading unit testing framework for PHP.
Over the years, PHPUnit has seen numerous major releases, adapting to new PHP language features, evolving best practices, and improving performance and reporting capabilities. Its widespread adoption has made it an indispensable tool in the PHP ecosystem for ensuring code quality and enabling test-driven development.