@@ -64,6 +64,8 @@ public function testStart()
64
64
public function testFail (\Codeception \Event \FailEvent $ e )
65
65
{
66
66
$ cest = $ e ->getTest ();
67
+ //log suppressed exception in case of _after hook failure
68
+ $ this ->logPreviousException ($ e ->getFail ());
67
69
$ context = $ this ->extractContext ($ e ->getFail ()->getTrace (), $ cest ->getTestMethod ());
68
70
// Do not attempt to run _after if failure was in the _after block
69
71
// Try to run _after but catch exceptions to prevent them from overwriting original failure.
@@ -93,7 +95,9 @@ function () use ($cest) {
93
95
if (!empty ($ errors )) {
94
96
foreach ($ errors as $ error ) {
95
97
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 ();
97
101
$ context = $ this ->extractContext ($ stack , $ cest ->getTestMethod ());
98
102
// Do not attempt to run _after if failure was in the _after block
99
103
// Try to run _after but catch exceptions to prevent them from overwriting original failure.
@@ -150,6 +154,31 @@ public function extractContext($trace, $class)
150
154
return null ;
151
155
}
152
156
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
+
153
182
/**
154
183
* Codeception event listener function, triggered before step.
155
184
* Check if it's a new page.
0 commit comments