Skip to content

Commit 315616f

Browse files
committed
MQE-1703: Implicit Suite Generation for Tests
1 parent 3c7ed4b commit 315616f

File tree

3 files changed

+75
-12
lines changed

3 files changed

+75
-12
lines changed

src/Magento/FunctionalTestingFramework/Console/BaseGenerateCommand.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\FunctionalTestingFramework\Util\Filesystem\DirSetupUtil;
1515
use Magento\FunctionalTestingFramework\Util\TestGenerator;
1616
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
17+
use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler;
1718

1819
class BaseGenerateCommand extends Command
1920
{
@@ -67,4 +68,35 @@ protected function removeGeneratedDirectory(OutputInterface $output, bool $verbo
6768
}
6869
}
6970
}
71+
72+
/**
73+
* Returns a 2D array of tests with their suites references that can be encoded into a json test configuration
74+
* @param array $tests
75+
* @return false|string
76+
* @throws \Magento\FunctionalTestingFramework\Exceptions\XmlException
77+
*/
78+
79+
protected function getTestAndSuiteConfiguration(array $tests)
80+
{
81+
$testConfiguration['tests'] = [];
82+
$testConfiguration['suites'] = [];
83+
$allSuiteTests = SuiteObjectHandler::getInstance()->getAllTestReferences();
84+
$suiteGroup = [];
85+
86+
foreach($tests as $test) {
87+
if (array_key_exists($test, $allSuiteTests)) {
88+
$suiteGroup[$test] = $allSuiteTests[$test];
89+
}
90+
else $testConfiguration['tests'][] = $test;
91+
}
92+
93+
foreach ($suiteGroup as $test => $suites) {
94+
95+
foreach ($suites as $suite) {
96+
$testConfiguration['suites'][$suite][] = $test;
97+
}
98+
99+
}
100+
return $testConfiguration;
101+
}
70102
}

src/Magento/FunctionalTestingFramework/Console/GenerateTestsCommand.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
6565
{
6666
$tests = $input->getArgument('name');
6767
$config = $input->getOption('config');
68-
$json = $input->getOption('tests');
68+
$json = $input->getOption('tests'); // for backward compatibility
6969
$force = $input->getOption('force');
7070
$time = $input->getOption('time') * 60 * 1000; // convert from minutes to milliseconds
7171
$debug = $input->getOption('debug') ?? MftfApplicationConfig::LEVEL_DEVELOPER; // for backward compatibility
7272
$remove = $input->getOption('remove');
7373
$verbose = $output->isVerbose();
7474
$allowSkipped = $input->getOption('allowSkipped');
7575

76+
// if test configuration is not specified, set implicitly
77+
if ($json === null && !empty($tests)) {
78+
$json = json_encode($this->getTestAndSuiteConfiguration($tests));
79+
}
80+
7681
if ($json !== null && !json_decode($json)) {
7782
// stop execution if we have failed to properly parse any json passed in by the user
7883
throw new TestFrameworkException("JSON could not be parsed: " . json_last_error_msg());
@@ -100,9 +105,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
100105
$testManifest->createTestGroups($time);
101106
}
102107

103-
if (empty($tests)) {
104-
SuiteGenerator::getInstance()->generateAllSuites($testManifest);
105-
}
108+
SuiteGenerator::getInstance()->generateAllSuites($testManifest);
106109

107110
$testManifest->generate();
108111

src/Magento/FunctionalTestingFramework/Console/RunTestCommand.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,34 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6464
}
6565

6666
if (!$skipGeneration) {
67+
$testConfiguration = $this->getTestAndSuiteConfiguration($tests);
6768
$command = $this->getApplication()->find('generate:tests');
6869
$args = [
69-
'--tests' => json_encode([
70-
'tests' => $tests,
71-
'suites' => null
72-
]),
70+
'--tests' => json_encode($testConfiguration),
7371
'--force' => $force,
7472
'--remove' => $remove,
7573
'--debug' => $debug,
7674
'--allowSkipped' => $allowSkipped
7775
];
7876
$command->run(new ArrayInput($args), $output);
7977
}
80-
78+
// tests with resolved suite references
79+
$resolvedTests = $this->getResolvedTests($testConfiguration);
8180
$returnCode = 0;
8281
$codeceptionCommand = realpath(PROJECT_ROOT . '/vendor/bin/codecept') . ' run functional ';
8382
$testsDirectory = TESTS_MODULE_PATH . DIRECTORY_SEPARATOR . TestGenerator::GENERATED_DIR . DIRECTORY_SEPARATOR;
8483
//execute only tests specified as arguments in run command
85-
foreach ($tests as $test) {
86-
$testGroup = TestGenerator::DEFAULT_DIR . DIRECTORY_SEPARATOR;
87-
$testName = $test . 'Cest.php';
84+
foreach ($resolvedTests as $test){
85+
// for tests in suite, set directory as suite name
86+
if (strpos($test, ':' )) {
87+
list($suite, $testName) = explode(":", $test);
88+
}
89+
// for standalone tests set directory as "default"
90+
else {
91+
list($suite, $testName) = [TestGenerator::DEFAULT_DIR, $test];
92+
}
93+
$testGroup = $suite . DIRECTORY_SEPARATOR;
94+
$testName .= 'Cest.php';
8895
if (!realpath($testsDirectory . $testGroup . $testName)) {
8996
throw new TestFrameworkException(
9097
$testName . " is not available under " . $testsDirectory . $testGroup
@@ -104,4 +111,25 @@ function ($type, $buffer) use ($output) {
104111
}
105112
return $returnCode;
106113
}
114+
115+
/** Get an array of tests with resolved suite references from $testConfiguration
116+
* eg: if test is referenced in a suite, it'll be stored in format "SuiteName:Testname";
117+
* if not it'll be stored as is.
118+
* @param $testConfiguration
119+
* @return array
120+
*/
121+
private function getResolvedTests($testConfiguration)
122+
{
123+
$testsArray = $testConfiguration['tests'] ?? [];
124+
$suitesArray = $testConfiguration['suites'] ?? [];
125+
$testArrayBuilder = [];
126+
foreach ($suitesArray as $suite => $tests) {
127+
$testArrayBuilder = array_merge($testArrayBuilder,
128+
array_map(function($test) use ($suite)
129+
{ return $suite . ':' . $test ; }, $tests));
130+
}
131+
return array_merge($testArrayBuilder, $testsArray);
132+
133+
134+
}
107135
}

0 commit comments

Comments
 (0)