PHPUnit with assertEqual


Just as was in the case of the SimpleTest, PHPUnit also has an assertEqual method that allows the separation of the expected value and the actual result.

The first parameter of the assertEqual method is the expected value and the second parameter is the actual result. Making the roles of the two values clear is important so in the reporting we can already know what was the expected and what was the actual result.


examples/php/phpunit/calc03.php
<?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));
    }
    public function testAddThree() {
        $this->assertEquals(3, add(1, 1, 1));
    }
}


?>

Output


PHPUnit 3.3.17 by Sebastian Bergmann.

.F

Time: 0 seconds

There was 1 failure:

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

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

So the difference between this output and the one from the first example is that in this case the reader can know more details about the failure. The reader can actually see that the expected values was 3 while the actual value was 2. That can already give a clue where the problem might be.