About
Installation
Command Line
Get the phar file for your php version. See Requirement
- Powershell
cd phpHome
wget https://phar.phpunit.de/phpunit.phar -OutFile phpunit.phar
wget https://phar.phpunit.de/phpunit-7.5.9.phar -OutFile phpunit.phar # for the 7.5.9
php phpunit.phar --version
- bash
cd phpHome
wget https://phar.phpunit.de/phpunit.phar
php phpunit.phar --version
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.
Management
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');
}
}
Annotations to group, filter test
/**
@group <group specification>
@author <author specification>
*/
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)
Others
Skipping test
$this->markTestSkipped('Skipped !' );
Testing for exception
- A runtime test on an exception that should be thrown
try {
functionThatShouldThrownAnException();
$this->fail("Exception should be thrown");
} catch (ExceptionNotFound $e) {
// other assertion or simply nothing
}