This tutorial will show you:

  • How to properly use $this->assert( )
  • How to figure out which code to test
  • How to figure out what to name test functions

Set up Test Directory

Your environment needs three things: * PHP + * xdebug * PHPUnit

To install PHPUnit:

wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
sudo mv phpunit.phar /usr/local/bin/phpunit

Then, make sure that in your project’s root directory, you have a subdirectory named tests. This is where your unit tests will be stored.

Each class will have its own unit test. The tests should be named by their class names with Test appended at the end. For example, ShoppingCartTest.php is the name of the unit test for class ShoppingCart.

Proper use of $this->assert ( )

Think of $this->assert ( ) as the test trying to assume a certain condition or list of conditions are true. You put the expected output inside assert ( ). The test will be performed against that data.

Assertions are meant to be used only in debugging they should not in appear in run time code.

Which code is tested? ==

You must test for each action in every function of the class. If there is a method involved, it needs to be tested and named properly.

How to name test functions?

Test functions should be named to accomplish exactly what they are supposed to do. Because of how the test documents are written, you want your function name to be the expected output. In the above example, we have a class ShoppingCart:

There are three steps for creating a unit test suite.

  • Identify the classes involved.
  • Create the test cases.
  • Register the test cases in the appropriate test suites.

Identify the classes involved

  1. Analyze the planned or existing code changes for a feature.
  2. Locate the classes involved.
  3. If the code is purely procedural, consider whether it is worth wrapping it up in a dummy class inside the test suite itself.

Create the test cases

  • Create the test suite with the class’ path mirrored in test.
  • E.g. src/api/gui/GUI.php -> test/src/api/gui/GUITest.php
  • Use the asserts found in [http://www.phpunit.de/manual/3.5/en/index.html the PHPUnit 3.5 documentation].
  • Use the following skeleton code:
class UserTest extends PHPUnit_Framework_TestCase
{
    /** @var wcmUser **/
    private $user;

    /** runs once before each test case in this class */
    protected function setUp()
    {
        ob_start();
    }

    /** runs once after each test case in this class.
        Be sure to cleanse the database changes your
        test cases have made! But **only** those changes.
    */
    protected function tearDown()
    {
        ob_end_clean();
    }
    
    /** First unit test **/
    public function testWillNotLoginWithAnInvalidUsername()
    {
    }
    
    public function testWillNotLoginWithAnInvalidPassword()
    {
    }
}

Register the test cases =

  1. Add the unit tests directly into ”’test/ProjectTestSuite.php”’.

Example: ”test/ProjectTestSuite.php”

/**
 * Static test suite.
 */
class ProjectTestSuite extends PHPUnit_Framework_TestSuite {
    public function __construct() {
        $this->setName('ProjectTestSuite');
        $this->addTestSuite('GUITest');
        $this->addTestSuite('UserTest');
    }
    /**
     * Creates the suite.
     */
    public static function suite() {
        return new self ( );
    }
}
Running the tests =

Running the tests is pretty straightforward:

    cd test/
    phpunit ProjectTestSuite.php

For more execution details:

    phpunit --debug ProjectTestSuite.php

To construct technical documentation from unit test function names:

    phpunit --testdox ProjectTestSuite.php