Php - PhpUnit

> Procedural Languages > Php

1 - About

Advertising

3 - Installation

3.1 - Command Line

Get the phar file

wget https://phar.phpunit.de/phpunit.phar
php phpunit.phar --version

3.2 - Idea/WebStorm Configuration

  • Load from include_path. You need to set it for Idea. See below or the Pear webpage.

  • Path to phpunit.phar: the location of the phpunit.phar must be given.

  • Custom Working Directory. Generally, if the project include others technology, the php code is located in a child folder. You can set the default in the default.

4 - Management

4.1 - Creating a test case

The TestClasses:

  • have the following name pattern classNameTest.php. For example, if the class to test is Myclass.php, the tests for it will be in MyclassTest.php.
  • are inherited from PHPUnit_Framework_TestCase.

The unit test are public methods that:

  • have the following prefix: test.
  • or have the @test annotation in a method's docblock
class MyClassTest extends PHPUnit_Framework_TestCase
{
 
    public function test()
    {
        $this->assertEquals('foo', 'foo');
    }
}
Advertising

4.2 - Annotations to group, filter test

/**
    @group <group specification>
    @author <author specification>
*/

4.3 - Run

# To run all test in the classFile  tests/myClassTest 
phpunit --bootstrap src/autoload.php tests/myClassTest 
 
# Or to execute all tests found declared in *Test.php sourcecode files in the tests directory.
phpunit --bootstrap src/autoload.php tests
 
# Or to execute all tests in a group
phpunit --bootstrap src/autoload.php --group plugins
 
# Or to exclude a group of test
phpunit --bootstrap src/autoload.php --exclude-group slow,internet

where:

  • phpunit is the runner
  • --bootstrap src/autoload.php is a bootstrap script commonly used to set up load the classes that are to be tested before execution. Example: autoload.php. .

Example of output:

PHPUnit 3.7.21 by Sebastian Bergmann.

Time: 0 seconds, Memory: 2.00Mb

OK (1 test, 1 assertion)

5 - Others

5.1 - Skipping test

$this->markTestSkipped('Skipped !' );

6 - Documentation / Reference

Advertising