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 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
blog comments powered by Disqus |
|
|
|
|
|
|
|