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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT