Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

assetEqual with message

assertEqual can actually get a third parameter too. This can hold a short description of what is being tested.

<?php


require_once(dirname(__FILE__) . '/../includes/mylib.php');

require_once 'PHPUnit/Framework.php';

class AddTest extends PHPUnit_Framework_TestCase {

    public function testAddTwo() {
        $this->assertEquals(2, add(1, 1), '1+1=2');
    }
    public function testAddThree() {
        $this->assertEquals(3, add(1, 1, 1), '1+1+1=3');
    }
}


?>

Output

In case of success this does not make a difference but when the test fails PHPUnit will display this message along with the name of the test function and the name of the test class. If the message is worded carefully it can further help in understanding the failure without even looking at the source code.

PHPUnit 3.3.17 by Sebastian Bergmann.

.F

Time: 0 seconds

There was 1 failure:

1) testAddThree(AddTest)
1+1+1=3
Failed asserting that <integer:2> matches expected value <integer:3>.
/home/gabor/work/gabor/training/testing/examples/php/phpunit/calc04.php:14

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.