PHP
  Home arrow PHP arrow Page 3 - 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? 
Google.com  
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 - Adding Multiple Tests
    ( Page 3 of 4 )

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek