Home arrow PHP arrow Page 3 - Unit Testing

Adding Multiple Tests - PHP

Having a formalized unit testing infrastructure for your projects will save you time in the long run, especially when looking for bugs in code. This article introduces you to setting up a unit testing framework. It is excerpted from chapter 6 of the book Advanced PHP Programming, written by George Schlossnagle (Sams; ISBN: 0672325616).

TABLE OF CONTENTS:
  1. Unit Testing
  2. An Introduction to Unit Testing
  3. Adding Multiple Tests
  4. Inline Packaging
By: Sams Publishing
Rating: starstarstarstarstar / 6
October 19, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

When you have a number of small test cases (for example, when checking that both the local part and the domain are split out correctly), you can avoid having to create a huge number of TestCase classes. To aid in this, a TestCase class can support multiple tests:

class EmailAddressTestCase extends
PHPUnit_Framework_TestCase{ public function _ _constructor($name) { parent::_ _constructor($name); } public function testLocalPart() { $email = new EmailAddress("george@omniti.com"); // check that the local part of the address is
equal to 'george' $this->assertTrue($email->localPart == 'george'); } public function testDomain() { $email = new EmailAddress("george@omniti.com"); $this->assertEquals($email->domain, 'omniti.com'); } }

Multiple tests are registered the same way as a single one:

$suite = new PHPUnit_FrameWork_TestSuite();
$suite->addTest(new
EmailAddressTestCase('testLocalPart')); $suite->addTest(new
EmailAddressTestCase('testDomain')); PHPUnit_TextUI_TestRunner::run($suite);

As a convenience, if you instantiate the PHPUnit_Framework_TestSuite object with the name of the TestCase class, $suite automatically causes any methods whose names begin with test to automatically register:

$suite = new
PHPUnit_Framework_TestSuite('EmailAddressTestCase'); // testLocalPart and testDomain are now
auto-registered PHPUnit_TextUI_TestRunner::run($suite);

Note that if you add multiple tests to a suite by using addTest, the tests will be run in the order in which they were added. If you autoregister the tests, they will be registered in the order returned by get_class_methods() (which is how TestSuite extracts the test methods automatically).

Writing Inline and Out-of-Line Unit Tests

Unit tests are not only useful in initial development, but throughout the full life of a project. Any time you refactor code, you would like to be able to verify its correctness by running the full unit test suite against it. How do you best arrange unit tests so that they are easy to run, keep up-to-date, and carry along with the library?

There are two options for packaging unit tests. In the first case, you can incorporate your testing code directly into your libraries. This helps ensure that tests are kept up-to-date with the code they are testing, but it also has some drawbacks. The other option is to package your tests in separate files.



 
 
>>> More PHP Articles          >>> More By Sams Publishing
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap

Dev Shed Tutorial Topics: