Home >Backend Development >PHP Tutorial >phpmaster | Getting Started with PHPUnit
This tutorial is outdated. For a current introduction to PHPUnit, please refer to our recently published updated guide.
Automating website testing is crucial for efficient development. Unit testing streamlines this process, preventing bugs introduced by code updates. This article provides a foundational understanding of PHPUnit, guiding you through your first unit test.
Before starting, ensure PHPUnit is installed. Instructions are available in the PHPUnit manual at https://www.php.cn/link/991c0955da231335e4864d3389698fd5.
Creating Your First Test
We'll start with a simple PHP class representing a user:
<?php class User { protected $name; public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function talk() { return "Hello world!"; } }
To test the user's greeting, we create a test class, UserTest
. Test class names generally mirror the classes being tested. The test class includes the tested class and PHPUnit's autoloading:
<?php require_once "PHPUnit/Autoload.php"; require_once "User.php"; class UserTest extends PHPUnit_Framework_TestCase { }
Each test is a method within this class. We use assertEquals()
to verify the greeting:
<?php ... class UserTest extends PHPUnit_Framework_TestCase { public function testTalk() { $user = new User(); $expected = "Hello world!"; $actual = $user->talk(); $this->assertEquals($expected, $actual); } }
Utilizing PHPUnit Fixtures
Repeatedly setting up objects in each test method is inefficient. PHPUnit fixtures establish a state before each test and reset it afterward. We override setUp()
to create and initialize the user:
<?php ... class UserTest extends PHPUnit_Framework_TestCase { protected $user; protected function setUp() { $this->user = new User(); $this->user->setName("Tom"); } }
tearDown()
unsets the user after each test:
<?php ... class UserTest extends PHPUnit_Framework_TestCase { ... protected function tearDown() { unset($this->user); } }
Now, testTalk()
simplifies to:
<?php ... class UserTest extends PHPUnit_Framework_TestCase { ... public function testTalk() { $expected = "Hello world!"; $actual = $this->user->talk(); $this->assertEquals($expected, $actual); } }
Running Your Tests
Run tests from the terminal using phpunit
. A "." indicates a successful test, while "F" signifies failure. Other characters represent errors, skipped tests, or incomplete tests.
Handling Test Failures
Modifying the User
class to return "blubb" instead of "Hello world!" will cause the test to fail, providing detailed error information.
Conclusion
This introduction demonstrates the simplicity of PHPUnit. Explore its capabilities further; experiment, learn from errors, and consult the PHPUnit manual for advanced techniques and assertion methods. The provided code (available on GitHub) uses Composer for dependency management; the require
statement should call vendor/autoload.php
. Run tests from the tests
directory using ../vendor/bin/phpunit UnitTest UserTest.php
.
Image via Archipoch / Shutterstock
The above is the detailed content of phpmaster | Getting Started with PHPUnit. For more information, please follow other related articles on the PHP Chinese website!