@@ -36,7 +36,6 @@ public function _initialize()
36
36
{
37
37
$ events = [
38
38
Events::TEST_START => 'testStart ' ,
39
- Events::TEST_FAIL => 'testFail ' ,
40
39
Events::STEP_AFTER => 'afterStep ' ,
41
40
Events::TEST_END => 'testEnd ' ,
42
41
Events::RESULT_PRINT_AFTER => 'saveFailed '
@@ -57,18 +56,7 @@ public function testStart()
57
56
}
58
57
59
58
/**
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.
72
60
* @param \Codeception\Event\TestEvent $e
73
61
* @return void
74
62
* @throws \Exception
@@ -77,20 +65,28 @@ public function testEnd(\Codeception\Event\TestEvent $e)
77
65
{
78
66
$ cest = $ e ->getTest ();
79
67
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
81
69
$ testResultObject = call_user_func (\Closure::bind (
82
70
function () use ($ cest ) {
83
71
return $ cest ->getTestResultObject ();
84
72
},
85
73
$ cest
86
74
));
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 ());
94
90
}
95
91
}
96
92
}
@@ -116,22 +112,38 @@ public function extractContext($trace, $class)
116
112
}
117
113
118
114
/**
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 .
120
116
* @param \Exception $exception
117
+ * @param String $testMethod
121
118
* @return mixed
122
119
*/
123
- public function logPreviousException ( \ Exception $ exception )
120
+ public function attachExceptionToAllure ( $ exception, $ testMethod )
124
121
{
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
125
136
$ change = function () {
126
137
if ($ this instanceof \PHPUnit \Framework \ExceptionWrapper) {
127
138
return $ this ->previous ;
128
139
} else {
129
140
return $ this ->getPrevious ();
130
141
}
131
142
};
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 );
135
147
}
136
148
}
137
149
0 commit comments