Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MQE-1087: Test _after hook does not run if test fails with exception #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 6, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class TestContextExtension extends \Codeception\Extension
*/
public static $events = [
Events::TEST_FAIL => 'testFail',
Events::STEP_AFTER => 'afterStep'
Events::STEP_AFTER => 'afterStep',
Events::TEST_END => 'testError'
];

/**
Expand All @@ -38,22 +39,61 @@ public function testFail(\Codeception\Event\FailEvent $e)
// Do not attempt to run _after if failure was in the _after block
// Try to run _after but catch exceptions to prevent them from overwriting original failure.
if ($context != TestContextExtension::TEST_PHASE_AFTER) {
try {
$actorClass = $e->getTest()->getMetadata()->getCurrent('actor');
$I = new $actorClass($cest->getScenario());
call_user_func(\Closure::bind(
function () use ($cest, $I) {
$cest->executeHook($I, 'after');
},
null,
$cest
));
} catch (\Exception $e) {
// Do not rethrow Exception
$this->runAfterBlock($e, $cest);
}
}

/**
* Codeception event listener function, triggered on test error.
* @param \Codeception\Event\TestEvent $e
* @return void
*/
public function testError(\Codeception\Event\TestEvent $e)
{
$cest = $e->getTest();

//Access private TestResultObject to find stack and if there are any errors (as opposed to failures)
$testResultObject = call_user_func(\Closure::bind(
function () use ($cest) {
return $cest->getTestResultObject();
},
$cest
));
$errors = $testResultObject->errors();
if (!empty($errors)) {
$stack = $errors[0]->thrownException()->getTrace();
$context = $this->extractContext($stack, $cest->getTestMethod());
// Do not attempt to run _after if failure was in the _after block
// Try to run _after but catch exceptions to prevent them from overwriting original failure.
if ($context != TestContextExtension::TEST_PHASE_AFTER) {
$this->runAfterBlock($e, $cest);
}
}
}

/**
* Runs cest's after block, if necessary.
* @param Symfony\Component\EventDispatcher\Event $e
* @param \Codeception\TestInterface $cest
* @return void
*/
private function runAfterBlock($e, $cest)
{
try {
$actorClass = $e->getTest()->getMetadata()->getCurrent('actor');
$I = new $actorClass($cest->getScenario());
call_user_func(\Closure::bind(
function () use ($cest, $I) {
$cest->executeHook($I, 'after');
},
null,
$cest
));
} catch (\Exception $e) {
// Do not rethrow Exception
}
}

/**
* Extracts hook method from trace, looking specifically for the cest class given.
* @param array $trace
Expand Down