@@ -64,6 +64,8 @@ public function testStart()
6464 public function testFail (\Codeception \Event \FailEvent $ e )
6565 {
6666 $ cest = $ e ->getTest ();
67+ //log suppressed exception in case of _after hook failure
68+ $ this ->logPreviousException ($ e ->getFail ());
6769 $ context = $ this ->extractContext ($ e ->getFail ()->getTrace (), $ cest ->getTestMethod ());
6870 // Do not attempt to run _after if failure was in the _after block
6971 // Try to run _after but catch exceptions to prevent them from overwriting original failure.
@@ -93,7 +95,9 @@ function () use ($cest) {
9395 if (!empty ($ errors )) {
9496 foreach ($ errors as $ error ) {
9597 if ($ error ->failedTest ()->getTestMethod () == $ cest ->getName ()) {
96- $ stack = $ errors [0 ]->thrownException ()->getTrace ();
98+ //log suppressed exception in case of _after hook failure
99+ $ this ->logPreviousException ($ error ->thrownException ());
100+ $ stack = $ error ->thrownException ()->getTrace ();
97101 $ context = $ this ->extractContext ($ stack , $ cest ->getTestMethod ());
98102 // Do not attempt to run _after if failure was in the _after block
99103 // Try to run _after but catch exceptions to prevent them from overwriting original failure.
@@ -150,6 +154,31 @@ public function extractContext($trace, $class)
150154 return null ;
151155 }
152156
157+ /**
158+ * Attach suppressed exception thrown before _after hook to the current step.
159+ * @param \Exception $exception
160+ * @return mixed
161+ */
162+ public function logPreviousException (\Exception $ exception )
163+ {
164+ $ change = function (){
165+ if ($ this instanceof \PHPUnit \Framework \ExceptionWrapper ) {
166+ return $ this ->previous ;
167+ }
168+ else {
169+ return $ this ->getPrevious ();
170+ }
171+ };
172+ $ firstException = $ change ->call ($ exception );
173+ $ bind = function () use ($ firstException ){
174+ $ exception = $ firstException ;
175+ };
176+ $ bind ->call ($ exception );
177+ if ($ firstException !== null ) {
178+ AllureHelper::addAttachmentToCurrentStep ($ firstException , 'Exception ' );
179+ }
180+ }
181+
153182 /**
154183 * Codeception event listener function, triggered before step.
155184 * Check if it's a new page.
0 commit comments