Skip to content

Allow extension to fail a test immediately after the test is executed #5226

@ruudk

Description

@ruudk

I'm trying to create an extension for PHPUnit 10 that runs immediately after the test is executed.

$this->testResult = $this->runTest();

Currently, only PostConditionFinished can be used for this. But this only works when the TestCase has an assertPostConditions method. When that method is not defined, it will never call PostConditionFinished.

I want to be able to do extra assertions for a library that I'm working on. I don't want to require a trait or abstract TestCase that sets up this assertPostConditions method.

When the assertions fail in my extension, they let the test fail, which is exactly what I want.

The benefit of running the extension immediately after runTest() is that any exception thrown by the extension, will be caught by this try/catch block:

try {
$this->checkRequirements();
$hasMetRequirements = true;
if ($this->inIsolation) {
$this->invokeBeforeClassHookMethods($hookMethods, $emitter);
}
if (method_exists(static::class, $this->name) &&
MetadataRegistry::parser()->forMethod(static::class, $this->name)->isDoesNotPerformAssertions()->isNotEmpty()) {
$this->doesNotPerformAssertions = true;
}
$this->invokeBeforeTestHookMethods($hookMethods, $emitter);
$this->invokePreConditionHookMethods($hookMethods, $emitter);
$emitter->testPrepared(
$this->valueObjectForEvents()
);
$this->wasPrepared = true;
$this->testResult = $this->runTest();
$this->verifyMockObjects();
$this->invokePostConditionHookMethods($hookMethods, $emitter);
$this->status = TestStatus::success();

Another solution could be to introduce a new Event that would allow an extension to fail the test after the test was passed. This was also discussed here: phpspec/prophecy-phpunit#45 (comment)

References:

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature/eventsIssues related to PHPUnit's event systemtype/enhancementA new idea that should be implementedversion/10Something affects PHPUnit 10

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions