|
5 | 5 | */
|
6 | 6 | namespace Magento\FunctionalTestingFramework\Allure\Adapter;
|
7 | 7 |
|
| 8 | +use Codeception\Codecept; |
| 9 | +use Codeception\Test\Cest; |
8 | 10 | use Codeception\Step\Comment;
|
9 | 11 | use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler;
|
10 | 12 | use Magento\FunctionalTestingFramework\Test\Objects\ActionGroupObject;
|
11 | 13 | use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
|
12 | 14 | use Magento\FunctionalTestingFramework\Util\TestGenerator;
|
| 15 | +use Yandex\Allure\Adapter\Model\Failure; |
| 16 | +use Yandex\Allure\Adapter\Model\Provider; |
13 | 17 | use Yandex\Allure\Adapter\Model\Status;
|
14 | 18 | use Yandex\Allure\Adapter\Model\Step;
|
| 19 | +use Yandex\Allure\Adapter\Allure; |
15 | 20 | use Yandex\Allure\Codeception\AllureCodeception;
|
16 | 21 | use Yandex\Allure\Adapter\Event\StepStartedEvent;
|
17 | 22 | use Yandex\Allure\Adapter\Event\StepFinishedEvent;
|
18 | 23 | use Yandex\Allure\Adapter\Event\StepFailedEvent;
|
19 | 24 | use Yandex\Allure\Adapter\Event\TestCaseFailedEvent;
|
20 | 25 | use Yandex\Allure\Adapter\Event\TestCaseFinishedEvent;
|
21 | 26 | use Yandex\Allure\Adapter\Event\TestCaseBrokenEvent;
|
| 27 | +use Yandex\Allure\Adapter\Event\AddAttachmentEvent; |
22 | 28 | use Codeception\Event\FailEvent;
|
23 | 29 | use Codeception\Event\SuiteEvent;
|
24 | 30 | use Codeception\Event\StepEvent;
|
| 31 | +use Codeception\Event\TestEvent; |
25 | 32 |
|
26 | 33 | /**
|
27 | 34 | * Class MagentoAllureAdapter
|
@@ -114,6 +121,7 @@ private function sanitizeGroupName($group)
|
114 | 121 | // if we can't find this group in the generated suites we have to assume that the group was split for generation
|
115 | 122 | $groupNameSplit = explode("_", $group);
|
116 | 123 | array_pop($groupNameSplit);
|
| 124 | + array_pop($groupNameSplit); |
117 | 125 | $originalName = implode("_", $groupNameSplit);
|
118 | 126 |
|
119 | 127 | // confirm our original name is one of the existing suite names otherwise just return the original group name
|
@@ -245,18 +253,25 @@ public function testError(FailEvent $failEvent)
|
245 | 253 |
|
246 | 254 | /**
|
247 | 255 | * Override of parent method, polls stepStorage for testcase and formats it according to actionGroup nesting.
|
248 |
| - * |
| 256 | + * @param TestEvent $testEvent |
| 257 | + * @throws \Yandex\Allure\Adapter\AllureException |
249 | 258 | * @return void
|
250 | 259 | */
|
251 |
| - public function testEnd() |
| 260 | + public function testEnd(TestEvent $testEvent) |
252 | 261 | {
|
| 262 | + $test = $this->getLifecycle()->getTestCaseStorage()->get(); |
| 263 | + // update testClass label to consolidate re-try reporting |
| 264 | + $this->formatAllureTestClassName($test); |
| 265 | + // Peek top of testCaseStorage to check of failure |
| 266 | + $testFailed = $test->getFailure(); |
253 | 267 | // Pops top of stepStorage, need to add it back in after processing
|
254 | 268 | $rootStep = $this->getLifecycle()->getStepStorage()->pollLast();
|
255 | 269 | $formattedSteps = [];
|
256 | 270 | $actionGroupStepContainer = null;
|
257 | 271 |
|
258 | 272 | $actionGroupStepKey = null;
|
259 | 273 | foreach ($rootStep->getSteps() as $step) {
|
| 274 | + $this->removeAttachments($step, $testFailed); |
260 | 275 | $stepKey = str_replace($actionGroupStepKey, '', $step->getName());
|
261 | 276 | if ($stepKey !== '[]' && $stepKey !== null) {
|
262 | 277 | $step->setName($stepKey);
|
@@ -309,9 +324,28 @@ function () use ($rootStep, $formattedSteps) {
|
309 | 324 |
|
310 | 325 | $this->getLifecycle()->getStepStorage()->put($rootStep);
|
311 | 326 |
|
| 327 | + $this->addAttachmentEvent($testEvent); |
| 328 | + |
312 | 329 | $this->getLifecycle()->fire(new TestCaseFinishedEvent());
|
313 | 330 | }
|
314 | 331 |
|
| 332 | + /** |
| 333 | + * Fire add attachment event |
| 334 | + * @param TestEvent $testEvent |
| 335 | + * @throws \Yandex\Allure\Adapter\AllureException |
| 336 | + * @return void |
| 337 | + */ |
| 338 | + private function addAttachmentEvent(TestEvent $testEvent) |
| 339 | + { |
| 340 | + // attachments supported since Codeception 3.0 |
| 341 | + if (version_compare(Codecept::VERSION, '3.0.0') > -1 && $testEvent->getTest() instanceof Cest) { |
| 342 | + $artifacts = $testEvent->getTest()->getMetadata()->getReports(); |
| 343 | + foreach ($artifacts as $name => $artifact) { |
| 344 | + Allure::lifecycle()->fire(new AddAttachmentEvent($artifact, $name, null)); |
| 345 | + } |
| 346 | + } |
| 347 | + } |
| 348 | + |
315 | 349 | /**
|
316 | 350 | * Reads action group stepKey from step.
|
317 | 351 | *
|
@@ -354,4 +388,60 @@ private function retrieveStepKey($stepLine)
|
354 | 388 |
|
355 | 389 | return $stepKey;
|
356 | 390 | }
|
| 391 | + |
| 392 | + /** |
| 393 | + * Removes attachments from step depending on MFTF configuration |
| 394 | + * @param Step $step |
| 395 | + * @param Failure $testFailed |
| 396 | + * @return void |
| 397 | + */ |
| 398 | + private function removeAttachments($step, $testFailed) |
| 399 | + { |
| 400 | + //Remove Attachments if verbose flag is not true AND test did not fail |
| 401 | + if (getenv('VERBOSE_ARTIFACTS') !== "true" && $testFailed === null) { |
| 402 | + foreach ($step->getAttachments() as $index => $attachment) { |
| 403 | + $step->removeAttachment($index); |
| 404 | + unlink(Provider::getOutputDirectory() . DIRECTORY_SEPARATOR . $attachment->getSource()); |
| 405 | + } |
| 406 | + } |
| 407 | + } |
| 408 | + |
| 409 | + /** |
| 410 | + * Format testClass label to consolidate re-try reporting for groups split for parallel execution |
| 411 | + * @param TestCase $test |
| 412 | + * @return void |
| 413 | + */ |
| 414 | + private function formatAllureTestClassName($test) |
| 415 | + { |
| 416 | + if ($this->getGroup() !== null) { |
| 417 | + foreach ($test->getLabels() as $name => $label) { |
| 418 | + if ($label->getName() == 'testClass') { |
| 419 | + $originalTestClass = $this->sanitizeTestClassLabel($label->getValue()); |
| 420 | + call_user_func(\Closure::bind( |
| 421 | + function () use ($label, $originalTestClass) { |
| 422 | + $label->value = $originalTestClass; |
| 423 | + }, |
| 424 | + null, |
| 425 | + $label |
| 426 | + )); |
| 427 | + break; |
| 428 | + } |
| 429 | + } |
| 430 | + } |
| 431 | + } |
| 432 | + |
| 433 | + /** |
| 434 | + * Function which sanitizes testClass label for split group runs |
| 435 | + * @param string $testClass |
| 436 | + * @return string |
| 437 | + */ |
| 438 | + private function sanitizeTestClassLabel($testClass) |
| 439 | + { |
| 440 | + $originalTestClass = $testClass; |
| 441 | + $originalGroupName = $this->sanitizeGroupName($this->getGroup()); |
| 442 | + if ($originalGroupName !== $this->getGroup()) { |
| 443 | + $originalTestClass = str_replace($this->getGroup(), $originalGroupName, $testClass); |
| 444 | + } |
| 445 | + return $originalTestClass; |
| 446 | + } |
357 | 447 | }
|
0 commit comments