PHP
  Home arrow PHP arrow Page 3 - Unit Testing in Detail
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 in Detail
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2006-10-26


    Table of Contents:
  • Unit Testing in Detail
  • Additional Features in PHPUnit
  • Adding More Test Conditions
  • Adding Listeners

  • 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 in Detail - Adding More Test Conditions
    ( Page 3 of 4 )

    With a bit of effort, you can evaluate the success or failure of any test by using assertTrue. Having to manipulate all your tests to evaluate as a truth statement is painful, so this section provides a nice selection of alternative assertions.

    The following example tests whether $actual is equal to $expected by using ==:

    assertEquals($expected, $actual, $message='')

    If $actual is not equal to $expected, a failure is generated, with an optional message.

    The following example:

    $this->assertTrue($email->localPart === 'george');

    is identical to this example:

    $this->assertEquals($email->localPart, 'george');

    The following example fails, with an optional message if $object is null:

    assertNotNull($object, $message = '')

    The following example fails, with an optional message if $object is not null:

    assertNull($object, $message = '')

    The following example tests whether $actual is equal to $expected, by using ===:

    assertSame($expected, $actual, $message='')

    If $actual is not equal to $expected, a failure is generated, with an optional message.

    The following example tests whether $actual is equal to $expected, by using ===:

    assertNotSame($expected, $actual, $message='')

    If $actual is equal to $expected, a failure is generated, with an optional message.

    The following example tests whether $condition is true:

    assertFalse($condition, $message='')

    If it is true, a failure is generated, with an optional message.

    The following returns a failure, with an optional message, if $actual is not matched by the PCRE $expected:

    assertRegExp($expected, $actual, $message='')

    For example, here is an assertion that $ip is a dotted-decimal quad:

    // returns true if $ip is 4 digits separated
    by '.'s (like an ip address) $this->assertRegExp('/\d+\.\d+\.\d+\.\d+/',$ip);

    The following example generates a failure, with an optional message:

    fail($message='')

    The following examples generates a success:

    pass()

    Using the setUp() and tearDown() Methods

    Many tests can be repetitive. For example, you might want to test EmailAddress with a number of different email addresses. As it stands, you are creating a new object in every test method. Ideally, you could consolidate this work and perform it only once. Fortunately, TestCase has the setUp and tearDown methods to handle just this case. setUp() is run immediately before the test methods in a TestCase are run, and tearDown() is run immediately afterward.

    To convert EmailAddress.phpt to use setUp(), you need to centralize all your prep work:

    class EmailAddressTestCase extends
    PHPUnit_Framework_TestCase{ protected $email; protected $localPart; protected $domain; function _ _construct($name) { parent::_ _construct($name); } function setUp() { $this->email = new
    EmailAddress("george@omniti.com"); $this->localPart = 'george'; $this->domain = 'omniti.com'; } function testLocalPart() { $this->assertEquals($this->email->localPart,
    $this->localPart, "localParts: ".$this->email->localPart. " of ".$this->email->address." != $this->localPart"); } function testDomain() { $this->assertEquals($this->email->domain,
    $this->domain, "domains: ".$this->email->domain. " of $this->email->address != $this->domain"); } }



     
     
    >>> 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek