phpunit
phpunit copied to clipboard
Add the number of assertions, passed and failed tests to TestSuite Finished event
Since PHPunit 10 we need to use events and extensions to write plugins. I'm currently writing a plugin where I would need the total amount of assertions, passed and failed tests after the tests have ran.
There's a numberOfAssertionsPerformed() method on the Test lifecycle Finished event, but I think it would be useful to add this to the finished event of the TestSuite as well?
final class Finished implements Event
{
private readonly Telemetry\Info $telemetryInfo;
private readonly TestSuite $testSuite;
public function __construct(Telemetry\Info $telemetryInfo, TestSuite $testSuite)
{
$this->telemetryInfo = $telemetryInfo;
$this->testSuite = $testSuite;
}
public function telemetryInfo(): Telemetry\Info
{
return $this->telemetryInfo;
}
public function testSuite(): TestSuite
{
return $this->testSuite;
}
public function numberOfAssertionsPerformed(): int
{
return $this->numberOfAssertionsPerformed;
}
public function numberOfTestPassed(): int
{
return $this->numberOfAssertionsPerformed;
}
public function numberOfTestsFailed(): int
{
return $this->numberOfAssertionsPerformed;
}
public function asString(): string
{
$name = '';
if (!empty($this->testSuite->name())) {
$name = $this->testSuite->name() . ', ';
}
return sprintf(
'Test Suite Finished (%s%d test%s)',
$name,
$this->testSuite->count(),
$this->testSuite->count() !== 1 ? 's' : ''
);
}
}
Or am I missing something and is this already available somewhere else?
@robiningelbrecht
You could subscribe to the
-
PHPUnit\Event\Test\AssertionFailed -
PHPUnit\Event\Test\AssertionSucceeded -
PHPUnit\Event\Test\Failed -
PHPUnit\Event\Test\Succeeded
events and collect the number of failed and succeeded assertions and tests.
Would that help?