-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
I'm trying to create an extension for PHPUnit 10 that runs immediately after the test is executed.
phpunit/src/Framework/TestCase.php
Line 623 in ddf1b95
| $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:
phpunit/src/Framework/TestCase.php
Lines 602 to 628 in ddf1b95
| 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: