assertEqual showing the actual values


SimpleTest and its UnitTestCase class provides further methods for assertions with better error reporting. In the end they all report assertions but these others have better capabilities in providing details on the failures.

In our case we could use assertEqual method instead of the assertTrue method. This one should receive two values. One of them should be the expected value the other one the actual result. The library does not make a recommendation which is which, it treats the two values in the same way and only checks if they are equal or not.


examples/php/simpletest/st03.php
<?php

require_once(dirname(__FILE__) . '/../../../tools/simpletest/autorun.php');

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

class TestOfAdd extends UnitTestCase {
    function testAdd() {
        $this->assertEqual(add(1,1), 2);
        $this->assertEqual(add(2,2), 4);
        $this->assertEqual(add(1,1,1), 3);
    }
}


?>

Output:


examples/php/simpletest/st03.txt
st03.php
Fail: TestOfAdd -> testAdd ->
  Equal expectation fails because [Integer: 2] differs from [Integer: 3] by 1
     at [.../examples/php/simpletest/st03.php line 11]
1/1 test cases complete: 2 passes, 1 fails and 0 exceptions.

With the last row being RED

As you can see there is now a better explanation of what failed.