As described in the PHPUnit doc, you can implement PHPUnit\Framework\Test
to write custom tests.
Sound great, but how can I launch those tests as part of my testsuite?
Given the following test directory:
+tests/
|- NormalTestExtendingTestCaseTest.php
|- CustomTestImplementingTest.php
phpunit.xml
And my phpunit.xml file:
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="My Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
Only NormalTestExtendingTestCaseTest.php is executed by PHPUnit.
CustomTestImplementingTest.php
<?php
use PHPUnit\Framework\TestCase;
class CustomTestImplementingTest implements PHPUnit\Framework\Test
{
public function count()
{
return 1;
}
public function run(PHPUnit\Framework\TestResult $result = null)
{
if ($result === null) {
$result = new PHPUnit\Framework\TestResult;
}
$result->startTest($this);
// do something
$result->endTest($this, $stopTime);
return $result;
}
}