Skip to content

Commit c8ac8bf

Browse files
committed
MQE-2669: Seprated a run:failed command to generate:failed and run:failed
1 parent 9fb3fdc commit c8ac8bf

File tree

3 files changed

+54
-54
lines changed

3 files changed

+54
-54
lines changed

src/Magento/FunctionalTestingFramework/Console/RunTestFailedCommand.php

+27-53
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
class RunTestFailedCommand extends BaseGenerateCommand
2020
{
21+
const DEFAULT_TEST_GROUP = 'default';
22+
2123
/**
2224
* @var string
2325
*/
24-
private $testsManifestFile;
26+
private $testsReRunFile = "";
2527

2628
/**
2729
* @var array
@@ -54,22 +56,14 @@ protected function configure()
5456
*/
5557
protected function execute(InputInterface $input, OutputInterface $output): int
5658
{
57-
$returnCode = $this->executeGenerateFailed($input, $output);
58-
if ($returnCode !== 0) {
59-
return $returnCode;
60-
}
61-
6259
$this->testsFailedFile = $this->getTestsOutputDir() . self::FAILED_FILE;
6360
$this->testsReRunFile = $this->getTestsOutputDir() . "rerun_tests";
6461

65-
$this->testsManifestFile= FilePathFormatter::format(TESTS_MODULE_PATH) .
66-
"_generated" .
67-
DIRECTORY_SEPARATOR .
68-
"testManifest.txt";
62+
$failedTests = $this->readFailedTestFile($this->testsFailedFile);
63+
$testManifestList = $this->filterTestsForExecution($failedTests);
6964

70-
$testManifestList = $this->readTestManifestFile();
71-
if ($testManifestList === false) {
72-
// If there is no test manifest file then we have nothing to execute.
65+
if (empty($testManifestList)) {
66+
// If there is no tests in manifest then we have nothing to execute.
7367
return 0;
7468
}
7569
$returnCode = 0;
@@ -113,51 +107,31 @@ function ($type, $buffer) use ($output) {
113107
}
114108

115109
/**
116-
* Execute generate failed tests command as a separate process, so that we can kill it and avoid high memory usage.
110+
* Returns a list of tests/suites which should have an additional run.
117111
*
118-
* @param InputInterface $input
119-
* @param OutputInterface $output
120-
* @return integer
121-
*
122-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
123-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
112+
* @param $failedTests
113+
* @return array
124114
*/
125-
private function executeGenerateFailed(InputInterface $input, OutputInterface $output)
115+
private function filterTestsForExecution($failedTests): array
126116
{
127-
$returnCode = 0;
128-
$binMftf = PROJECT_ROOT . '/vendor/bin/mftf';
129-
if (file_exists($binMftf) === false) {
130-
$binMftf = PROJECT_ROOT . '/bin/mftf';
131-
}
132-
$forceGenerate = $input->getOption('force') ? ' -f' : '';
133-
$mftfCommand = realpath($binMftf) . ' generate:failed' . $forceGenerate;
134-
135-
$process = new Process($mftfCommand);
136-
$process->setWorkingDirectory(TESTS_BP);
137-
$process->setIdleTimeout(600);
138-
$process->setTimeout(0);
139-
$returnCode = max($returnCode, $process->run(
140-
function ($type, $buffer) use ($output) {
141-
$output->write($buffer);
117+
$testsOrGroupsToRerun = [];
118+
119+
foreach ($failedTests as $test) {
120+
if (!empty($test)) {
121+
$this->writeFailedTestToFile($test, $this->testsReRunFile);
122+
$testInfo = explode(DIRECTORY_SEPARATOR, $test);
123+
$suiteName = $testInfo[count($testInfo) - 2];
124+
list($testPath) = explode(":", $test);
125+
126+
if ($suiteName === self::DEFAULT_TEST_GROUP) {
127+
$testsOrGroupsToRerun[] = $testPath;
128+
} else {
129+
$testsOrGroupsToRerun[] = "-g " . $suiteName;
130+
}
142131
}
143-
));
144-
$process->__destruct();
145-
unset($process);
146-
147-
return $returnCode;
148-
}
149-
150-
/**
151-
* Returns an array of run commands read from the manifest file created post generation
152-
*
153-
* @return array|boolean
154-
*/
155-
private function readTestManifestFile()
156-
{
157-
if (file_exists($this->testsManifestFile)) {
158-
return file($this->testsManifestFile, FILE_IGNORE_NEW_LINES);
159132
}
160-
return false;
133+
134+
return $testsOrGroupsToRerun;
161135
}
162136

163137
/**

src/Magento/FunctionalTestingFramework/Suite/views/SuiteClass.mustache

+18
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class {{suiteName}} extends \Codeception\GroupObject
4343
{{#before}}
4444
public function _before(\Codeception\Event\TestEvent $e)
4545
{
46+
$this->testCount = $this->getTestCount();
4647
$this->webDriver = $this->getModule('\Magento\FunctionalTestingFramework\Module\MagentoWebDriver');
4748
$this->moduleContainer = $this->webDriver->getModuleContainer();
4849
{{#helpers}}
@@ -171,4 +172,21 @@ class {{suiteName}} extends \Codeception\GroupObject
171172
}
172173
return $module;
173174
}
175+
176+
/**
177+
* Counts how many tests in group.
178+
*
179+
* @return integer
180+
*/
181+
private function getTestCount()
182+
{
183+
$config = $this->getGlobalConfig();
184+
if (empty($config['groups']) && empty($config['groups'][self::$group])) {
185+
return $this->testCount;
186+
}
187+
$pathToGroupDir = TESTS_BP . DIRECTORY_SEPARATOR . array_first($config['groups'][self::$group]);
188+
$pathToGroupCests = $pathToGroupDir . DIRECTORY_SEPARATOR . "*Cest.php";
189+
190+
return count(glob($pathToGroupCests));
191+
}
174192
}

src/Magento/FunctionalTestingFramework/Util/TestGenerator.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -1801,7 +1801,8 @@ private function generateTestPhp($test)
18011801
} else {
18021802
$skipString .= "No issues have been specified.";
18031803
}
1804-
$steps = "\t\t" . '$scenario->skip("' . $skipString . '");' . "\n";
1804+
$steps = "\t\t" . 'unlink(__FILE__);' . "\n";
1805+
$steps .= "\t\t" . '$scenario->skip("' . $skipString . '");' . "\n";
18051806
$dependencies .= ', \Codeception\Scenario $scenario';
18061807
}
18071808

@@ -1811,6 +1812,13 @@ private function generateTestPhp($test)
18111812
$testPhp .= $steps;
18121813
$testPhp .= "\t}\n";
18131814

1815+
$testPhp .= PHP_EOL;
1816+
$testPhp .= sprintf("\tpublic function _passed(%s)\n", $dependencies);
1817+
$testPhp .= "\t{\n";
1818+
$testPhp .= "\t\t// Deleting itself so that we can rerun only failed tests." . PHP_EOL;
1819+
$testPhp .= "\t\tunlink(__FILE__);" . PHP_EOL;
1820+
$testPhp .= "\t}\n";
1821+
18141822
return $testPhp;
18151823
}
18161824

0 commit comments

Comments
 (0)