Getting Started with PHPUnit: Installation, Configuration, and Common Assertions
This guide introduces PHPUnit for PHP, explains how to install and configure it with Composer, and demonstrates common assertions such as assertEquals, assertNotEmpty, assertFalse, assertIsNumeric, and assertSame through practical real-world code examples.
PHPUnit is an open-source unit testing framework for PHP that supports test‑driven development (TDD). TDD is a software development approach where tests are written before code. PHPUnit provides a set of features to support TDD, including assertions, test cases, test suites, fixtures, mock objects, data providers, and code‑coverage reports.
Setting up PHPUnit with Composer
Run the following command to require PHPUnit as a development dependency: composer require --dev phpunit/phpunit Then generate a phpunit.xml configuration file with: ./vendor/bin/phpunit --generate-configuration The generated phpunit.xml should contain the appropriate configuration (illustrated in the original image). Create a tests directory and a test class file, e.g., CalculatorTest.php, that extends TestCase. Test classes must end with the word “test” and test methods must start with “test”.
Run a test class with:
phpunit CalculatorTest.phpExample Assertions
The following sections demonstrate common PHPUnit assertions with syntax and sample code.
1. assertEquals
Syntax:
assertEquals(mixed $expected, mixed $actual, string $message = '')Example:
public function testAddSuccess() {
$addSuccess = new Calculate(1, 2, 'add');
$returnValue = $addSuccess->calcMethod();
$this->assertEquals(3, $returnValue);
}This checks that $returnValue equals 3; otherwise the test fails.
2. assertNotEmpty
Syntax:
assertNotEmpty(mixed $actual, string $message = '')Example:
public function testSubNotEmpty() {
$addSuccess = new Calculate(10, 5, 'sub');
$returnValue = $addSuccess->calcMethod();
$this->assertNotEmpty($returnValue);
}The assertion fails if $returnValue is empty.
3. assertFalse
Syntax:
assertFalse(bool $condition, string $message = '')Example:
public function testSendWrongMethod() {
$addSuccess = new Calculate(10, 5, 'test');
$returnValue = $addSuccess->calcMethod();
$this->assertFalse($returnValue);
}The test succeeds when the method returns false.
4. assertIsNumeric
Syntax: assertIsNumeric($actual, string $message = '') Example:
public function testIsNumericResult() {
$addSuccess = new Calculate(2, 3, 'mul');
$returnValue = $addSuccess->calcMethod();
$this->assertIsNumeric($returnValue);
}Ensures that $returnValue is of numeric type.
5. assertSame
Syntax:
assertSame(mixed $expected, mixed $actual, string $message = '')Example:
public function testSameError() {
$addSuccess = new Calculate('aa', 3, 'mul');
$returnValue = $addSuccess->calcMethod();
$this->assertSame('To be calculated variable must be numeric!', $returnValue);
}The test fails if $expected and $actual do not have identical value and type.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
