Skip to content

Commit 5788253

Browse files
committed
MQE-1181: Fatal error while test running
- Moved reset session to after all test runs - changed suites to not run suite_after if last test was a failure (compatibility issues)
1 parent 17f0153 commit 5788253

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

src/Magento/FunctionalTestingFramework/Extension/TestContextExtension.php

+17-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
class TestContextExtension extends \Codeception\Extension
1818
{
1919
const TEST_PHASE_AFTER = "_after";
20+
// @codingStandardsIgnoreStart
21+
const MAGENTO_WEB_DRIVER_CLASS = "\Magento\FunctionalTestingFramework\Module\MagentoWebDriver";
22+
// @codingStandardsIgnoreEnd
2023

2124
/**
2225
* Codeception Events Mapping to methods
@@ -43,7 +46,7 @@ public function testFail(\Codeception\Event\FailEvent $e)
4346
$this->runAfterBlock($e, $cest);
4447
}
4548
}
46-
49+
4750
/**
4851
* Codeception event listener function, triggered on test error.
4952
* @param \Codeception\Event\TestEvent $e
@@ -62,14 +65,21 @@ function () use ($cest) {
6265
));
6366
$errors = $testResultObject->errors();
6467
if (!empty($errors)) {
65-
$stack = $errors[0]->thrownException()->getTrace();
66-
$context = $this->extractContext($stack, $cest->getTestMethod());
67-
// Do not attempt to run _after if failure was in the _after block
68-
// Try to run _after but catch exceptions to prevent them from overwriting original failure.
69-
if ($context != TestContextExtension::TEST_PHASE_AFTER) {
70-
$this->runAfterBlock($e, $cest);
68+
foreach ($errors as $error) {
69+
if ($error->failedTest()->getTestMethod() == $cest->getName()) {
70+
$stack = $errors[0]->thrownException()->getTrace();
71+
$context = $this->extractContext($stack, $cest->getTestMethod());
72+
// Do not attempt to run _after if failure was in the _after block
73+
// Try to run _after but catch exceptions to prevent them from overwriting original failure.
74+
if ($context != TestContextExtension::TEST_PHASE_AFTER) {
75+
$this->runAfterBlock($e, $cest);
76+
}
77+
continue;
78+
}
7179
}
7280
}
81+
// Reset Session and Cookies after all Test Runs, workaround due to functional.suite.yml restart: true
82+
$this->getModule(self::MAGENTO_WEB_DRIVER_CLASS)->_runAfter($e->getTest());
7383
}
7484

7585
/**
@@ -90,8 +100,6 @@ function () use ($cest, $I) {
90100
null,
91101
$cest
92102
));
93-
// Reset Session and Cookies, workaround due to functional.suite.yml restart: true
94-
$this->getModule(MagentoWebDriver::class)->_runAfter($e->getTest());
95103
} catch (\Exception $e) {
96104
// Do not rethrow Exception
97105
}

src/Magento/FunctionalTestingFramework/Suite/views/SuiteClass.mustache

+24-2
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,38 @@ class {{suiteName}} extends \Codeception\GroupObject
5959
{{#after}}
6060
public function _after(\Codeception\Event\TestEvent $e)
6161
{
62-
$this->executePostConditions();
62+
$this->executePostConditions($e);
6363
}
6464

6565

66-
private function executePostConditions()
66+
private function executePostConditions(\Codeception\Event\TestEvent $e)
6767
{
6868
if ($this->currentTestRun == $this->testCount) {
6969
print sprintf(self::$HOOK_EXECUTION_INIT, "after");
7070
7171
try {
72+
// Find out if Test in Suite failed, will cause potential failures in suite after
73+
$cest = $e->getTest();
74+
75+
//Access private TestResultObject to find stack and if there are any errors (as opposed to failures)
76+
$testResultObject = call_user_func(\Closure::bind(
77+
function () use ($cest) {
78+
return $cest->getTestResultObject();
79+
},
80+
$cest
81+
));
82+
$errors = $testResultObject->errors();
83+
84+
if (!empty($errors)) {
85+
foreach ($errors as $error) {
86+
if ($error->failedTest()->getTestMethod() == $cest->getName()) {
87+
// Do not attempt to run _after if failure was in the _after block
88+
// Try to run _after but catch exceptions to prevent them from overwriting original failure.
89+
\PHPUnit_Framework_Assert::fail("LAST TESTS FAILED IN SUITE, SUITE_AFTER CANNOT BE RUN");
90+
}
91+
}
92+
}
93+
7294
{{> testActions}}
7395
} catch (\Exception $exception) {
7496
print $exception->getMessage();

0 commit comments

Comments
 (0)