PHP
  Home arrow PHP arrow Page 2 - Unit Testing
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Unit Testing
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2006-10-19

    Table of Contents:
  • Unit Testing
  • An Introduction to Unit Testing
  • Adding Multiple Tests
  • Inline Packaging

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Unit Testing - An Introduction to Unit Testing


    (Page 2 of 4 )

    To be successful, a unit testing framework needs to have certain properties, including the following:

    • Automated—The system should run all the tests necessary with no interaction from the programmer.

    • Easy to write—The system must be easy to use.

    • Extensible—To streamline efforts and minimize duplication of work, you should be able to reuse existing tests when creating new ones.

    To actually benefit from unit testing, we need to make sure our tests have certain properties:

    • Comprehensive—Tests should completely test all function/class APIs. You should ensure not only that the function APIs work as expected, but also that they fail correctly when improper data is passed to them. Furthermore, you should write tests for any bugs discovered over the life of the library. Partial tests leave holes that can lead to errors when refactoring or to old bugs reappearing.

    • Reusable—Tests should be general enough to usefully test their targets again and again. The tests will be permanent fixtures that are maintained and used to verify the library over its entire life span.

    Writing Unit Tests for Automated Unit Testing

    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 Test

    A 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) =
    explode("@", $this->address); } }

    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
    PHPUnit_Framework_TestCase { public function _ _constructor($name) { parent::_ _constructor($name); } 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'); } }

    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
    EmailAddressTest('testLocalPart'));

    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)

    More PHP Articles
    More By Sams Publishing


       · This article is an excerpt from the book "Advanced PHP Programming," published by...
     

    Buy this book now. This article is excerpted from chapter 6 of the book Advanced PHP Programming, written by George Schlossnagle (Sams; ISBN: 0672325616). Check it out today at your favorite bookstore. Buy this book now.

       

    PHP ARTICLES

    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP
    - PHP Networking
    - Validating Web Forms with the Code Igniter P...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT