Skip to content

Commit ac507c8

Browse files
authored
Merge pull request #164 from magento-gl/MQE-1693
MQE-1693 | Ability To Run MFTF JSON Configuration From File
2 parents 8c85258 + 05a0162 commit ac507c8

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

docs/commands/mftf.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ vendor/bin/mftf generate:tests [option] [<test name>] [<test name>] [--remove]
190190
| `--force` | Forces test generation, regardless of the module merge order defined in the Magento instance. Example: `generate:tests --force`. |
191191
| `-i,--time` | Set time in minutes to determine the group size when `--config=parallel` is used. <br/>Example: `generate:tests --config=parallel --time=15` <br/>Option `--time` will be the default and the __default value__ is `10` when neither `--time` nor `--groups` is specified. <br/>Example: `generate:tests --config=parallel`|
192192
| `-g,--groups` | Set number of groups to be split into when `--config=parallel` is used. <br>Example: `generate:tests --config=parallel --groups=300` <br/>Options `--time` and `--groups` are mutually exclusive and only one should be used.|
193-
| `--tests` | Defines the test configuration as a JSON string.|
193+
| `--tests` | Defines the test configuration as a JSON string or JSON file path.|
194194
| `--allow-skipped` | Allows MFTF to generate and run tests marked with `<skip>.`|
195195
| `--debug` | Performs schema validations on XML files. <br/> DEFAULT: `generate:tests` implicitly performs schema validation on merged files. It does not indicate the file name where the error is encountered. <br/> DEVELOPER: `--debug` performs per-file validation and returns additional debug information (such as the filename where an error occurred) when test generation fails because of an invalid XML schema. This option takes extra processing time. Use it after test generation has failed once.<br/>|
196196
| `-r,--remove`| Removes the existing generated suites and tests cleaning up the `_generated` directory before the actual run. For example, `generate:tests SampleTest --remove` cleans up the entire `_generated` directory and generates `SampleTest` only.|
@@ -241,12 +241,20 @@ Complex configuration to generate a few non-suite tests, a single test in a suit
241241

242242
The command that encodes this complex configuration:
243243

244+
Command to generate test by json string:
245+
244246
```bash
245247
vendor/bin/mftf generate:tests --tests '{"tests":["general_test1","general_test2","general_test3"],"suites":{"sample":["suite_test1"],"sample2":null}}'
246248
```
247249

248250
Note that the strings must be escaped and surrounded in quotes.
249251

252+
Command to generate test by json file:
253+
254+
```bash
255+
vendor/bin/mftf generate:tests --tests ./foldername/filename.json
256+
```
257+
250258
### `generate:suite`
251259

252260
#### Description

src/Magento/FunctionalTestingFramework/Console/GenerateTestsCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected function configure()
6969
'tests',
7070
't',
7171
InputOption::VALUE_REQUIRED,
72-
'A parameter accepting a JSON string used to determine the test configuration'
72+
'A parameter accepting a JSON string or JSON file path used to determine the test configuration'
7373
)->addOption(
7474
'filter',
7575
null,
@@ -138,6 +138,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
138138
return 1;
139139
}
140140

141+
if ($json !== null && is_file($json)) {
142+
$json = file_get_contents($json);
143+
}
144+
141145
if (!empty($tests)) {
142146
$json = $this->getTestAndSuiteConfiguration($tests);
143147
}

src/Magento/FunctionalTestingFramework/Console/RunTestCommand.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
use Symfony\Component\Process\Process;
2121
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
2222

23+
/**
24+
* @SuppressWarnings(PHPMD)
25+
*/
2326
class RunTestCommand extends BaseGenerateCommand
2427
{
2528
/**
@@ -40,15 +43,19 @@ protected function configure()
4043
->setDescription("generation and execution of test(s) defined in xml")
4144
->addArgument(
4245
'name',
43-
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
46+
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
4447
"name of tests to generate and execute"
4548
)->addOption(
4649
'skip-generate',
4750
'k',
4851
InputOption::VALUE_NONE,
4952
"skip generation and execute existing test"
53+
)->addOption(
54+
'tests',
55+
't',
56+
InputOption::VALUE_REQUIRED,
57+
'A parameter accepting a JSON string or JSON file path used to determine the test configuration'
5058
);
51-
5259
parent::configure();
5360
}
5461

@@ -63,6 +70,7 @@ protected function configure()
6370
protected function execute(InputInterface $input, OutputInterface $output): int
6471
{
6572
$tests = $input->getArgument('name');
73+
$json = $input->getOption('tests'); // for backward compatibility
6674
$skipGeneration = $input->getOption('skip-generate');
6775
$force = $input->getOption('force');
6876
$remove = $input->getOption('remove');
@@ -86,7 +94,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8694
$allowSkipped
8795
);
8896

89-
$testConfiguration = $this->getTestAndSuiteConfiguration($tests);
97+
if ($json !== null) {
98+
if (is_file($json)) {
99+
$testConfiguration = file_get_contents($json);
100+
} else {
101+
$testConfiguration = $json;
102+
}
103+
}
104+
105+
if (!empty($tests)) {
106+
$testConfiguration = $this->getTestAndSuiteConfiguration($tests);
107+
}
108+
109+
if ($testConfiguration !== null && !json_decode($testConfiguration)) {
110+
// stop execution if we have failed to properly parse any json passed in by the user
111+
throw new TestFrameworkException("JSON could not be parsed: " . json_last_error_msg());
112+
}
90113

91114
$generationErrorCode = 0;
92115

@@ -98,7 +121,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
98121
'--remove' => $remove,
99122
'--debug' => $debug,
100123
'--allow-skipped' => $allowSkipped,
101-
'-v' => $verbose
124+
'-v' => $verbose,
125+
''
102126
];
103127
$command->run(new ArrayInput($args), $output);
104128

0 commit comments

Comments
 (0)