PHPUnit PHP Unit Testing Framework

PHPUnit is a testing framework for PHP.

Download from phpunit.de

You can also download it on Github. Here is the Git:

https://github.com/sebastianbergmann/phpunit.git

PHPUnit Composer Method

Add a dependency your project's composer.json file if you use Composer (dependency manager for PHP)

composer require --dev phpunit/phpunit ^6.0

Writing Tests for PHPUnit

This is from the PHPUnit documentation. This simple example shows how to test PHP's array operations.

use PHPUnitFrameworkTestCase;

class StackTest extends TestCase
{
public function testPushAndPop()
{
$stack = []; // create an empty array
$this->assertEquals(0, count($stack)); // test if the array $stack has 0 items

array_push($stack, 'foo'); // push an item called 'foo' into the array $stack
$this->assertEquals('foo', $stack[count($stack)-1]); // test the item at $stack[0] 1-1 is equal to 'foo'
$this->assertEquals(1, count($stack)); // test if the array $stack has 1 item

$this->assertEquals('foo', array_pop($stack)); // pop the 'foo' item from the array $stack
$this->assertEquals(0, count($stack)); // test if the array $stack contains 0 items
}
}

Test Dependencies

Unit Tests are primarily written as a good practice to help developers identify and fix bugs, to refactor code and to serve as documentation for a unit of software under test. To achieve these benefits, unit tests ideally should cover all the possible paths in a program. One unit test usually covers one specific path in one function or method. However, a test method is not necessary an encapsulated, independent entity. Often there are implicit dependencies between test methods, hidden in the implementation scenario of a test.  --Adrian Kuhn et. al.

 

Visit sunny St. George, Utah, USA