Home arrow PHP arrow Page 2 - Unit Testing in Detail

Additional Features in PHPUnit - PHP

Last week, we introduced you to setting up a unit testing framework. This week, you will learn about running multiple tests simultaneously, creating more informative error messages, and more. This article, the second of three parts, is excerpted from chapter 6 of the book Advanced PHP Programming, written by George Schlossnagle (Sams; ISBN: 0672325616).

TABLE OF CONTENTS:
  1. Unit Testing in Detail
  2. Additional Features in PHPUnit
  3. Adding More Test Conditions
  4. Adding Listeners
By: Sams Publishing
Rating: starstarstarstarstar / 2
October 26, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

One of the benefits of using an even moderately mature piece of open-source software is that it usually has a good bit of sugar—or ease-of-use features—in it. As more developers use it, convenience functions are added to suit developers' individual styles, and this often produces a rich array of syntaxes and features.


Feature Creep - The addition of features over time in both open-source and commercial software is often a curse as much as it is a blessing. As the feature set of an application grows, two unfortunate things often happen:

  • Some features become less well maintained than others. How do you then know which features are the best to use?

  • Unnecessary features bloat the code and hinder maintainability and performance.

Both of these problems and some strategies for combating them are discussed in Chapter 8, "Designing a Good API."


Creating More Informative Error Messages

Sometimes you would like a more informative message than this:

PHPUnit 1.0.0-dev by Sebastian Bergmann.
.F.
Time: 0.00583696365356
There was 1 failure:
1) TestCase emailaddresstestcase->testlocalpart()
failed: expected true, actual false FAILURES!!! Tests run: 2, Failures: 1, Errors: 0.

Especially when a test is repeated multiple times for different data, a more informative error message is essential to understanding where the break occurred and what it means. To make creating more informative error messages easy, all the assert functions that TestCase inherit from PHPUnit::Assert support free-form error messages. Instead of using this code:

function testLocalPart() {
$email = new EmailAddress("georg@omniti.com");
// check that the local part of the address is
equal to 'george' $this->assertTrue($email->localPart == 'george'); }

which generates the aforementioned particularly cryptic message, you can use a custom message:

function testLocalPart() {
$email = new EmailAddress("georg@omniti.com");
// check that the local part of the address is
equal to 'george' $this->assertTrue($email->localPart == 'george', "localParts: $email->localPart of $email->address
!= 'george'"); }

This produces the following much clearer error message:

PHPUnit 1.0.0-dev by Sebastian Bergmann.
.F.
Time: 0.00466096401215
There was 1 failure:
1) TestCase emailaddresstestcase->testlocalpart()
failed: local name: george of george@omniti.com != georg FAILURES!!! Tests run: 2, Failures: 1, Errors: 0.

Hopefully, by making the error message clearer, we can fix the typo in the test.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: