sebastianbergmann / phpunit

The PHP Unit Testing framework.
https://phpunit.de/
BSD 3-Clause "New" or "Revised" License
19.69k stars 2.2k forks source link

Add the number of assertions, passed and failed tests to TestSuite Finished event #5292

Open robiningelbrecht opened 1 year ago

robiningelbrecht commented 1 year ago

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?

localheinz commented 1 year ago

@robiningelbrecht

You could subscribe to the

events and collect the number of failed and succeeded assertions and tests.

Would that help?