assertTrue with PHPUnit


In the first example we'll try to solve the same simple problem as we did in the SimpleTest example. We have a function called add() in the mylib.php file. We would like to test that function.

We include that file in our test script and include the already installed PHPUnit/Framework.php

We have to create a class that extends PHPUnit_Framework_TestCase - the name of the class does not matter. In the class we need to create testing functions. The name of each function has to start with 'test', otherwise the name does not matter. It should be descriptive as any function name.


examples/php/phpunit/calc01.php
<?php


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

require_once 'PHPUnit/Framework.php';

class AddTest extends PHPUnit_Framework_TestCase {

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


?>

As opposed to the SimpleTest framework, in PHPUnit the primary way to execute the tests is from the command line using the phpunit script. so you do the following:


phpunit examples/php/phpunit/cal01.php

The output looks like this:


PHPUnit 3.3.17 by Sebastian Bergmann.

.F

Time: 0 seconds

There was 1 failure:

1) testAddThree(AddTest)
Failed asserting that <boolean:false> is true.
/home/gabor/work/gabor/training/testing/examples/php/phpunit/calc01.php:14

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

In the results you can see a single .F meaning one of the testing functions failed. In addition you'll see details of the failure giving both the name of of the test function and the test class.