Skip to content

Commit c5b651b

Browse files
committed
MQE-659: [ALLURE] Include the test "stepKey" in the MFTF Allure Report
1 parent 14a433a commit c5b651b

File tree

4 files changed

+146
-8
lines changed

4 files changed

+146
-8
lines changed

etc/config/codeception.dist.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ paths:
88
support: src/Magento/FunctionalTestingFramework
99
envs: etc/_envs
1010
settings:
11+
silent: true
1112
colors: true
1213
memory_limit: 1024M
1314
extensions:
1415
enabled:
16+
- Magento\FunctionalTestingFramework\Codeception\Subscriber\Console
1517
- Magento\FunctionalTestingFramework\Extension\TestContextExtension
1618
- Magento\FunctionalTestingFramework\Allure\Adapter\MagentoAllureAdapter
1719
config:

src/Magento/FunctionalTestingFramework/Allure/Adapter/MagentoAllureAdapter.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*/
66
namespace Magento\FunctionalTestingFramework\Allure\Adapter;
77

8+
use Codeception\Step\Comment;
89
use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler;
910
use Magento\FunctionalTestingFramework\Test\Objects\ActionGroupObject;
11+
use \Magento\FunctionalTestingFramework\Util\TestGenerator;
1012
use Yandex\Allure\Adapter\Model\Step;
1113
use Yandex\Allure\Codeception\AllureCodeception;
1214
use Yandex\Allure\Adapter\Event\StepStartedEvent;
@@ -31,6 +33,8 @@ class MagentoAllureAdapter extends AllureCodeception
3133
{
3234
const STEP_PASSED = "passed";
3335

36+
private $testFiles = [];
37+
3438
/**
3539
* Array of group values passed to test runner command
3640
*
@@ -116,6 +120,11 @@ public function stepBefore(StepEvent $stepEvent)
116120
{
117121
//Hard set to 200; we don't expose this config in MFTF
118122
$argumentsLength = 200;
123+
$stepKey = null;
124+
125+
if (!($stepEvent->getStep() instanceof Comment)) {
126+
$stepKey = $this->retrieveStepKey($stepEvent->getStep()->getLine());
127+
}
119128

120129
// DO NOT alter action if actionGroup is starting, need the exact actionGroup name for good logging
121130
if (strpos($stepEvent->getStep()->getAction(), ActionGroupObject::ACTION_GROUP_CONTEXT_START) !== false) {
@@ -130,7 +139,11 @@ public function stepBefore(StepEvent $stepEvent)
130139
$stepArgs = $stepEvent->getStep()->getMetaStep()->getArgumentsAsString($argumentsLength);
131140
}
132141

133-
$stepName = $stepAction . ' ' . $stepArgs;
142+
$stepName = '';
143+
if ($stepKey !== null) {
144+
$stepName .= '[' . $stepKey . '] ';
145+
}
146+
$stepName .= $stepAction . ' ' . $stepArgs;
134147

135148
// Strip control characters so that report generation does not fail
136149
$stepName = preg_replace('/[[:cntrl:]]/', '', $stepName);
@@ -220,4 +233,20 @@ function () use ($rootStep, $formattedSteps) {
220233

221234
$this->getLifecycle()->fire(new TestCaseFinishedEvent());
222235
}
236+
237+
private function retrieveStepKey($stepLine)
238+
{
239+
$stepKey = null;
240+
list($filePath, $stepLine) = explode(":", $stepLine);
241+
$prevStepLine = $stepLine - 2;
242+
243+
if (!array_key_exists($filePath, $this->testFiles)) {
244+
$this->testFiles[$filePath] = explode(PHP_EOL, file_get_contents($filePath));
245+
}
246+
$testFile = $this->testFiles[$filePath];
247+
248+
list($stepKey) = sscanf($testFile[$prevStepLine], TestGenerator::STEP_KEY_ANNOTATION);
249+
250+
return $stepKey;
251+
}
223252
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
namespace Magento\FunctionalTestingFramework\Codeception\Subscriber;
3+
4+
use Codeception\Event\FailEvent;
5+
use Codeception\Event\PrintResultEvent;
6+
use Codeception\Event\StepEvent;
7+
use Codeception\Event\SuiteEvent;
8+
use Codeception\Event\TestEvent;
9+
use Codeception\Events;
10+
use Codeception\Lib\Console\Message;
11+
use Codeception\Lib\Console\MessageFactory;
12+
use Codeception\Lib\Console\Output;
13+
use Codeception\Lib\Notification;
14+
use Codeception\Step;
15+
use Codeception\Step\Comment;
16+
use Codeception\Suite;
17+
use Codeception\Test\Descriptor;
18+
use Codeception\Test\Interfaces\ScenarioDriven;
19+
use Codeception\Util\Debug;
20+
use Magento\FunctionalTestingFramework\Util\TestGenerator;
21+
use Symfony\Component\Console\Output\OutputInterface;
22+
use Symfony\Component\Console\Formatter\OutputFormatter;
23+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
24+
25+
class Console extends \Codeception\Subscriber\Console
26+
{
27+
private $testFiles = [];
28+
29+
public function beforeStep(StepEvent $e)
30+
{
31+
if (!$this->steps or !$e->getTest() instanceof ScenarioDriven) {
32+
return;
33+
}
34+
$metaStep = $e->getStep()->getMetaStep();
35+
if ($metaStep and $this->metaStep != $metaStep) {
36+
$this->message(' ' . $metaStep->getPrefix())
37+
->style('bold')
38+
->append($metaStep->__toString())
39+
->writeln();
40+
}
41+
$this->metaStep = $metaStep;
42+
43+
$this->printStepKeys($e->getStep());
44+
}
45+
46+
private function printStepKeys(Step $step)
47+
{
48+
if ($step instanceof Comment and $step->__toString() == '') {
49+
return; // don't print empty comments
50+
}
51+
52+
$stepKey = $this->retrieveStepKey($step->getLine());
53+
54+
$msg = $this->message(' ');
55+
if ($this->metaStep) {
56+
$msg->append(' ');
57+
}
58+
if ($stepKey !== null) {
59+
$msg->append(OutputFormatter::escape("[" . $stepKey . "] "));
60+
}
61+
62+
if (!$this->metaStep) {
63+
$msg->style('bold');
64+
}
65+
66+
$msg->append(OutputFormatter::escape($step->toString($this->width)));
67+
if ($this->metaStep) {
68+
$msg->style('info');
69+
}
70+
$msg->writeln();
71+
}
72+
73+
/**
74+
* @param $string
75+
* @return Message
76+
*/
77+
private function message($string = '')
78+
{
79+
return $this->messageFactory->message($string);
80+
}
81+
82+
private function retrieveStepKey($stepLine)
83+
{
84+
$stepKey = null;
85+
list($filePath, $stepLine) = explode(":", $stepLine);
86+
$prevStepLine = $stepLine - 2;
87+
88+
if (!array_key_exists($filePath, $this->testFiles)) {
89+
$this->testFiles[$filePath] = explode(PHP_EOL, file_get_contents($filePath));
90+
}
91+
$testFile = $this->testFiles[$filePath];
92+
93+
list($stepKey) = sscanf($testFile[$prevStepLine], TestGenerator::STEP_KEY_ANNOTATION);
94+
95+
return $stepKey;
96+
}
97+
}

src/Magento/FunctionalTestingFramework/Util/TestGenerator.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class TestGenerator
3939
const SUITE_SCOPE = 'suite';
4040
const PRESSKEY_ARRAY_ANCHOR_KEY = '987654321098765432109876543210';
4141
const PERSISTED_OBJECT_NOTATION_REGEX = '/\${1,2}[\w.\[\]]+\${1,2}/';
42+
const STEP_KEY_ANNOTATION = "\t\t/** @stepKey %s */";
4243

4344
/**
4445
* Path to the export dir.
@@ -697,14 +698,19 @@ public function generateStepsPhp($actionObjects, $generationScope = TestGenerato
697698
if (isset($customActionAttributes['storeCode'])) {
698699
$storeCode = $customActionAttributes['storeCode'];
699700
}
701+
702+
if ($actionObject->getType() !== 'comment') {
703+
$testSteps .= PHP_EOL . sprintf(self::STEP_KEY_ANNOTATION, $stepKey) . PHP_EOL;
704+
}
700705
switch ($actionObject->getType()) {
701706
case "createData":
702707
$entity = $customActionAttributes['entity'];
703708
//Add an informative statement to help the user debug test runs
704709
$testSteps .= sprintf(
705-
"\t\t$%s->amGoingTo(\"create entity that has the stepKey: %s\");\n",
710+
"\t\t$%s->comment(\"[%s] create '%s' entity\");\n",
706711
$actor,
707-
$stepKey
712+
$stepKey,
713+
$entity
708714
);
709715

710716
//TODO refactor entity field override to not be individual actionObjects
@@ -759,8 +765,9 @@ public function generateStepsPhp($actionObjects, $generationScope = TestGenerato
759765
$key .= $actionGroup;
760766
//Add an informative statement to help the user debug test runs
761767
$contextSetter = sprintf(
762-
"\t\t$%s->amGoingTo(\"delete entity that has the createDataKey: %s\");\n",
768+
"\t\t$%s->comment(\"[%s] delete entity '%s'\");\n",
763769
$actor,
770+
$stepKey,
764771
$key
765772
);
766773

@@ -802,9 +809,11 @@ public function generateStepsPhp($actionObjects, $generationScope = TestGenerato
802809

803810
//Add an informative statement to help the user debug test runs
804811
$testSteps .= sprintf(
805-
"\t\t$%s->amGoingTo(\"update entity that has the createdDataKey: %s\");\n",
812+
"\t\t$%s->comment(\"[%s] update '%s' entity to '%s'\");\n",
806813
$actor,
807-
$key
814+
$stepKey,
815+
$key,
816+
$updateEntity
808817
);
809818

810819
// Build array of requiredEntities
@@ -848,9 +857,10 @@ public function generateStepsPhp($actionObjects, $generationScope = TestGenerato
848857
}
849858
//Add an informative statement to help the user debug test runs
850859
$testSteps .= sprintf(
851-
"\t\t$%s->amGoingTo(\"get entity that has the stepKey: %s\");\n",
860+
"\t\t$%s->comment(\"[%s] get '%s' entity\");\n",
852861
$actor,
853-
$stepKey
862+
$stepKey,
863+
$entity
854864
);
855865

856866
// Build array of requiredEntities

0 commit comments

Comments
 (0)