Skip to content

Commit fa579d1

Browse files
committed
MQE-1755: mftf run:test Test1 Test2 does not run before/after hooks correctly
1 parent 7222259 commit fa579d1

File tree

1 file changed

+64
-40
lines changed

1 file changed

+64
-40
lines changed

src/Magento/FunctionalTestingFramework/Console/RunTestCommand.php

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919

2020
class RunTestCommand extends BaseGenerateCommand
2121
{
22+
/**
23+
* The return code. Determined by all tests that run.
24+
*
25+
* @var integer
26+
*/
27+
private $returnCode = 0;
28+
2229
/**
2330
* Configures the current command.
2431
*
@@ -76,60 +83,77 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7683
];
7784
$command->run(new ArrayInput($args), $output);
7885
}
79-
// tests with resolved suite references
80-
$resolvedTests = $this->resolveSuiteReferences($testConfiguration);
8186

87+
$testConfigArray = json_decode($testConfiguration, true);
88+
89+
// run tests not referenced in suites
90+
$this->runTests($testConfigArray['tests'], $output);
91+
92+
// run tests in suites
93+
$this->runTestsInSuite($testConfigArray['suites'], $output);
94+
95+
return $this->returnCode;
96+
97+
}
98+
99+
/**
100+
* Run tests not referenced in suites
101+
* @param array $testsConfig
102+
* @param OutputInterface $output
103+
* @throws TestFrameworkException
104+
*/
105+
private function runTests($testsConfig, OutputInterface $output) {
106+
107+
108+
$tests = $testsConfig ?? [];
82109
$codeceptionCommand = realpath(PROJECT_ROOT . '/vendor/bin/codecept') . ' run functional ';
83-
$testsDirectory = TESTS_MODULE_PATH . DIRECTORY_SEPARATOR . TestGenerator::GENERATED_DIR . DIRECTORY_SEPARATOR;
84-
$returnCode = 0;
85-
//execute only tests specified as arguments in run command
86-
foreach ($resolvedTests as $test) {
87-
//set directory as suite name for tests in suite, if not set to "default"
88-
if (strpos($test, ':')) {
89-
list($testGroup, $testName) = explode(":", $test);
90-
} else {
91-
list($testGroup, $testName) = [TestGenerator::DEFAULT_DIR, $test];
92-
}
93-
$testGroup = $testGroup . DIRECTORY_SEPARATOR;
94-
$testName = $testName . 'Cest.php';
95-
if (!realpath($testsDirectory . $testGroup . $testName)) {
110+
$testsDirectory = TESTS_MODULE_PATH .
111+
DIRECTORY_SEPARATOR .
112+
TestGenerator::GENERATED_DIR .
113+
DIRECTORY_SEPARATOR .
114+
TestGenerator::DEFAULT_DIR .
115+
DIRECTORY_SEPARATOR ;
116+
117+
foreach ($tests as $test) {
118+
$testName = $test . 'Cest.php';
119+
if (!realpath($testsDirectory . $testName)) {
96120
throw new TestFrameworkException(
97-
$testName . " is not available under " . $testsDirectory . $testGroup
121+
$testName . " is not available under " . $testsDirectory
98122
);
99123
}
100-
$fullCommand = $codeceptionCommand . $testsDirectory . $testGroup . $testName . ' --verbose --steps';
124+
$fullCommand = $codeceptionCommand . $testsDirectory . $testName . ' --verbose --steps';
101125
$process = new Process($fullCommand);
102126
$process->setWorkingDirectory(TESTS_BP);
103127
$process->setIdleTimeout(600);
104128
$process->setTimeout(0);
105-
106-
$returnCode = max($returnCode, $process->run(
107-
function ($type, $buffer) use ($output) {
108-
$output->write($buffer);
109-
}
110-
));
129+
$subReturnCode = $process->run(function ($type, $buffer) use ($output) {
130+
$output->write($buffer);
131+
});
132+
$this->returnCode = max($this->returnCode, $subReturnCode);
111133
}
112-
return $returnCode;
113134
}
114135

115136
/**
116-
* Get an array of tests with resolved suite references from $testConfiguration
117-
* eg: if test is referenced in a suite, it'll be stored in format suite:test
118-
* @param string $testConfigurationJson
119-
* @return array
137+
* Run tests referenced in suites within suites' context.
138+
* @param array $suitesConfig
139+
* @param OutputInterface $output
120140
*/
121-
private function resolveSuiteReferences($testConfigurationJson)
122-
{
123-
$testConfiguration = json_decode($testConfigurationJson, true);
124-
$testsArray = $testConfiguration['tests'] ?? [];
125-
$suitesArray = $testConfiguration['suites'] ?? [];
126-
$testArrayBuilder = [];
127-
128-
foreach ($suitesArray as $suite => $tests) {
129-
foreach ($tests as $test) {
130-
$testArrayBuilder[] = "$suite:$test";
131-
}
141+
private function runTestsInSuite($suitesConfig, OutputInterface $output) {
142+
143+
$suites = $suitesConfig ?? [];
144+
$codeceptionCommand = realpath(PROJECT_ROOT . '/vendor/bin/codecept') . ' run functional --verbose --steps ';
145+
$testGroups = array_keys($suites);
146+
//for tests in suites, run them as a group to run before and after block
147+
foreach ($testGroups as $testGroup) {
148+
$fullCommand = $codeceptionCommand . " -g {$testGroup}";
149+
$process = new Process($fullCommand);
150+
$process->setWorkingDirectory(TESTS_BP);
151+
$process->setIdleTimeout(600);
152+
$process->setTimeout(0);
153+
$subReturnCode = $process->run(function ($type, $buffer) use ($output) {
154+
$output->write($buffer);
155+
});
156+
$this->returnCode = max($this->returnCode, $subReturnCode);
132157
}
133-
return array_merge($testArrayBuilder, $testsArray);
134158
}
135159
}

0 commit comments

Comments
 (0)