@@ -36,7 +36,6 @@ public function _initialize()
3636 {
3737 $ events = [
3838 Events::TEST_START => 'testStart ' ,
39- Events::TEST_FAIL => 'testFail ' ,
4039 Events::STEP_AFTER => 'afterStep ' ,
4140 Events::TEST_END => 'testEnd ' ,
4241 Events::RESULT_PRINT_AFTER => 'saveFailed '
@@ -57,18 +56,7 @@ public function testStart()
5756 }
5857
5958 /**
60- * Codeception event listener function, triggered on test failure.
61- * @param \Codeception\Event\FailEvent $e
62- * @return void
63- */
64- public function testFail (\Codeception \Event \FailEvent $ e )
65- {
66- //log suppressed exception in case of _after hook failure
67- $ this ->logPreviousException ($ e ->getFail ());
68- }
69-
70- /**
71- * Codeception event listener function, triggered on test ending (naturally or by error).
59+ * Codeception event listener function, triggered on test ending naturally or by errors/failures.
7260 * @param \Codeception\Event\TestEvent $e
7361 * @return void
7462 * @throws \Exception
@@ -77,20 +65,28 @@ public function testEnd(\Codeception\Event\TestEvent $e)
7765 {
7866 $ cest = $ e ->getTest ();
7967
80- //Access private TestResultObject to find stack and if there are any errors (as opposed to failures)
68+ //Access private TestResultObject to find stack and if there are any errors/ failures
8169 $ testResultObject = call_user_func (\Closure::bind (
8270 function () use ($ cest ) {
8371 return $ cest ->getTestResultObject ();
8472 },
8573 $ cest
8674 ));
87- $ errors = $ testResultObject ->errors ();
88- if (!empty ($ errors )) {
89- foreach ($ errors as $ error ) {
90- if ($ error ->failedTest ()->getTestMethod () == $ cest ->getName ()) {
91- //log suppressed exception in case of _after hook failure
92- $ this ->logPreviousException ($ error ->thrownException ());
93- continue ;
75+
76+ // check for errors in all test hooks and attach in allure
77+ if (!empty ($ testResultObject ->errors ())) {
78+ foreach ($ testResultObject ->errors () as $ error ) {
79+ if ($ error ->failedTest ()->getTestMethod () == $ cest ->getTestMethod ()) {
80+ $ this ->attachExceptionToAllure ($ error ->thrownException (), $ cest ->getTestMethod ());
81+ }
82+ }
83+ }
84+
85+ // check for failures in all test hooks and attach in allure
86+ if (!empty ($ testResultObject ->failures ())) {
87+ foreach ($ testResultObject ->failures () as $ failure ) {
88+ if ($ failure ->failedTest ()->getTestMethod () == $ cest ->getTestMethod ()) {
89+ $ this ->attachExceptionToAllure ($ failure ->thrownException (), $ cest ->getTestMethod ());
9490 }
9591 }
9692 }
@@ -116,22 +112,38 @@ public function extractContext($trace, $class)
116112 }
117113
118114 /**
119- * Attach suppressed exception thrown before _after hook to the current step .
115+ * Attach stack trace of exceptions thrown in each test hook to allure .
120116 * @param \Exception $exception
117+ * @param String $testMethod
121118 * @return mixed
122119 */
123- public function logPreviousException ( \ Exception $ exception )
120+ public function attachExceptionToAllure ( $ exception, $ testMethod )
124121 {
122+ $ exceptionType = null ;
123+ $ trace = null ;
124+
125+ if (is_subclass_of ($ exception , \PHPUnit \Framework \Exception::class)) {
126+ $ trace = $ exception ->getSerializableTrace ();
127+ } else {
128+ $ trace = $ exception ->getTrace ();
129+ }
130+
131+ $ context = $ this ->extractContext ($ trace , $ testMethod );
132+
133+ AllureHelper::addAttachmentToCurrentStep ($ exception , $ context . 'Exception ' );
134+
135+ //pop suppressed exceptions and attach to allure
125136 $ change = function () {
126137 if ($ this instanceof \PHPUnit \Framework \ExceptionWrapper) {
127138 return $ this ->previous ;
128139 } else {
129140 return $ this ->getPrevious ();
130141 }
131142 };
132- $ firstException = $ change ->call ($ exception );
133- if ($ firstException !== null ) {
134- AllureHelper::addAttachmentToCurrentStep ($ firstException , 'Exception ' );
143+ $ previousException = $ change ->call ($ exception );
144+
145+ if ($ previousException !== null ) {
146+ $ this ->attachExceptionToAllure ($ previousException , $ testMethod );
135147 }
136148 }
137149
0 commit comments