Home arrow PHP arrow Page 3 - Unit Testing in Detail

Adding More Test Conditions - 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

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
 

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: