PHPUnit PHP testing framework
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
composer require --dev phpunit/phpunit
vendor/bin/phpunit
vendor/bin/phpunit <path>
vendor/bin/phpunit --filter <name>
vendor/bin/phpunit --testdox
vendor/bin/phpunit --stop-on-failure
vendor/bin/phpunit --generate-configuration
vendor/bin/phpunit --coverage-text
Concepts
- phpunit.xml suites, source
- Test classes *Test.php
- Result dots, F, E, R
- Reports coverage, JUnit
Run and select
vendor/bin/phpunit
vendor/bin/phpunit <path>
vendor/bin/phpunit --filter <pattern>
--testsuite <name>
--group <name>
--exclude-group <name>
Execution
--stop-on-failure
--fail-on-warning
--order-by <order>
Output
--testdox--display-deprecations
--display-warnings
Coverage
--coverage-text--coverage-html <dir>
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html
<dir>
Configuration
--generate-configuration
-c <file><phpunit bootstrap="vendor/autoload.php" colors="true" failOnWarning="true" failOnRisky="true" cacheDirectory=".phpunit.cache"> <testsuites> <testsuite name="unit"><directory>tests/unit</directory></testsuite> </testsuites> <source><include><directory suffix=".php">src</directory></include></source></phpunit>
Writing tests
use PHPUnit\Framework\TestCase;final class CartTest extends TestCase{ protected function setUp(): void { $this->cart = new Cart; } #[Test] public function adds_items(): void { $this->cart->add('apple'); $this->assertCount(1, $this->cart); }}
assertSame($expected, $actual)
assertEquals($expected, $actual)
assertTrue($cond) / assertFalse($cond)
assertNull($v) / assertNotNull($v)
assertCount($n, $items)
assertInstanceOf(<Class>::class, $v)
assertEqualsWithDelta($a, $b, $delta)
$this->expectException(<Class>::class)
setUp() / tearDown()
setUpBeforeClass() / tearDownAfterClass()
Attributes
#[Test]#[DataProvider('<method>')]
#[TestWith([<args>])]
#[Group('<name>')]
#[DataProvider('sums')]public function test_sum(int $a, int $b, int $want): void{ $this->assertSame($want, $a + $b);}public static function sums(): array{ return ['zero' => [0, 0, 0], 'one' => [1, 0, 1]];}
Test doubles
$this->createStub(<Class>::class)
$this->createMock(<Class>::class)
$stub->method('<m>')->willReturn($v)
->willReturnMap([[<args>, $ret]])
->willThrowException(new <Ex>)
$mock->expects($this->once())->method('<m>')
Recipes
vendor/bin/phpunit --filter test_adds_items --testdox# add --stop-on-failure and --display-deprecations when debugging
XDEBUG_MODE=coverage vendor/bin/phpunit \ --coverage-html build/coverage --coverage-textxdg-open build/coverage/index.html
vendor/bin/phpunit --fail-on-warning --fail-on-risky \ --log-junit build/junit.xml --coverage-clover build/clover.xml



