To be successful, a unit testing framework needs to have certain properties, including the following:
To actually benefit from unit testing, we need to make sure our tests have certain properties:
For the testing framework discussed in this chapter, we will use PEAR's PHPUnit. PHPUnit, like most of the free unit testing frameworks, is based closely on JUnit, Erich Gamma and Kent Beck's excellent unit testing suite for Java. Installing PHPUnit is just a matter of running the following (which most likely needs root access): # pear install phpunit Alternatively, you can download PHPUnit from http://pear.php.net/PHPUnit. Writing Your First Unit TestA unit test consists of a collection of test cases. A test case is designed to check the outcome of a particular scenario. The scenario can be something as simple as testing the result of a single function or testing the result of a set of complex operations. A test case in PHPUnit is a subclass of the PHPUnit_Framework_TestCase class. An instance of PHPUnit_Framework_TestCase is one or several test cases, together with optional setup and tear-down code. The simplest test case implements a single test. Let's write a test to validate the behavior of a simple email address parser. The parser will break an RFC 822 email address into its component parts. class EmailAddress {
public $localPart;
public $domain;
public $address;
public function _ _construct($address = null) {
if($address) {
$this->address = $address;
$this->extract();
}
}
protected function extract() {
list($this->localPart, $this->domain) =
To create a test for this, you create a TestCase class that contains a method that tests that a known email address is correctly broken into its components: require_once "EmailAddress.inc"; require_once 'PHPUnit/Framework/TestClass.php'; class EmailAddressTest extends Then you need to register the test class. You instantiate a PHPUnit_Framework_ TestSuite object and the test case to it: require_omce "PHPUnit/Framework/TestSuite"; $suite = new PHPUnit_Framework_TestSuite(); $suite->addTest(new After you have done this, you run the test: require_once "PHPUnit/TextUI/TestRunner"; PHPUnit_TextUI_TestRunner::run($suite); You get the following results, which you can print: PHPUnit 1.0.0-dev by Sebastian Bergmann. . Time: 0.00156390666962 OK (1 test)
blog comments powered by Disqus |