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:
<code>composer require --dev phpunit/phpunit</code>Then generate a phpunit.xml configuration file with:
<code>./vendor/bin/phpunit --generate-configuration</code>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:
<code>phpunit CalculatorTest.php</code>Example Assertions
The following sections demonstrate common PHPUnit assertions with syntax and sample code.
1. assertEquals
Syntax:
<code>assertEquals(mixed $expected, mixed $actual, string $message = '')</code>Example:
<code>public function testAddSuccess() {
$addSuccess = new Calculate(1, 2, 'add');
$returnValue = $addSuccess->calcMethod();
$this->assertEquals(3, $returnValue);
}</code>This checks that $returnValue equals 3; otherwise the test fails.
2. assertNotEmpty
Syntax:
<code>assertNotEmpty(mixed $actual, string $message = '')</code>Example:
<code>public function testSubNotEmpty() {
$addSuccess = new Calculate(10, 5, 'sub');
$returnValue = $addSuccess->calcMethod();
$this->assertNotEmpty($returnValue);
}</code>The assertion fails if $returnValue is empty.
3. assertFalse
Syntax:
<code>assertFalse(bool $condition, string $message = '')</code>Example:
<code>public function testSendWrongMethod() {
$addSuccess = new Calculate(10, 5, 'test');
$returnValue = $addSuccess->calcMethod();
$this->assertFalse($returnValue);
}</code>The test succeeds when the method returns false.
4. assertIsNumeric
Syntax:
<code>assertIsNumeric($actual, string $message = '')</code>Example:
<code>public function testIsNumericResult() {
$addSuccess = new Calculate(2, 3, 'mul');
$returnValue = $addSuccess->calcMethod();
$this->assertIsNumeric($returnValue);
}</code>Ensures that $returnValue is of numeric type.
5. assertSame
Syntax:
<code>assertSame(mixed $expected, mixed $actual, string $message = '')</code>Example:
<code>public function testSameError() {
$addSuccess = new Calculate('aa', 3, 'mul');
$returnValue = $addSuccess->calcMethod();
$this->assertSame('To be calculated variable must be numeric!', $returnValue);
}</code>The test fails if $expected and $actual do not have identical value and type.
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.