Skip to content

Commit 88030f3

Browse files
authored
Merge pull request #448 from magento/MQE-1755
MQE-1755: mftf run:test Test1 Test2 does not run before/after hooks c…
2 parents fb444bd + 6a5798e commit 88030f3

File tree

1 file changed

+74
-45
lines changed

1 file changed

+74
-45
lines changed

src/Magento/FunctionalTestingFramework/Console/RunTestCommand.php

Lines changed: 74 additions & 45 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
*
@@ -44,8 +51,6 @@ protected function configure()
4451
* @param OutputInterface $output
4552
* @return integer
4653
* @throws \Exception
47-
*
48-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
4954
*/
5055
protected function execute(InputInterface $input, OutputInterface $output): int
5156
{
@@ -76,60 +81,84 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7681
];
7782
$command->run(new ArrayInput($args), $output);
7883
}
79-
// tests with resolved suite references
80-
$resolvedTests = $this->resolveSuiteReferences($testConfiguration);
8184

85+
$testConfigArray = json_decode($testConfiguration, true);
86+
87+
if (isset($testConfigArray['tests'])) {
88+
$this->runTests($testConfigArray['tests'], $output);
89+
}
90+
91+
if (isset($testConfigArray['suites'])) {
92+
$this->runTestsInSuite($testConfigArray['suites'], $output);
93+
}
94+
95+
return $this->returnCode;
96+
}
97+
98+
/**
99+
* Run tests not referenced in suites
100+
*
101+
* @param array $tests
102+
* @param OutputInterface $output
103+
* @return void
104+
* @throws TestFrameworkException
105+
*/
106+
private function runTests(array $tests, OutputInterface $output)
107+
{
82108
$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)) {
109+
$testsDirectory = TESTS_MODULE_PATH .
110+
DIRECTORY_SEPARATOR .
111+
TestGenerator::GENERATED_DIR .
112+
DIRECTORY_SEPARATOR .
113+
TestGenerator::DEFAULT_DIR .
114+
DIRECTORY_SEPARATOR ;
115+
116+
foreach ($tests as $test) {
117+
$testName = $test . 'Cest.php';
118+
if (!realpath($testsDirectory . $testName)) {
96119
throw new TestFrameworkException(
97-
$testName . " is not available under " . $testsDirectory . $testGroup
120+
$testName . " is not available under " . $testsDirectory
98121
);
99122
}
100-
$fullCommand = $codeceptionCommand . $testsDirectory . $testGroup . $testName . ' --verbose --steps';
101-
$process = new Process($fullCommand);
102-
$process->setWorkingDirectory(TESTS_BP);
103-
$process->setIdleTimeout(600);
104-
$process->setTimeout(0);
105-
106-
$returnCode = max($returnCode, $process->run(
107-
function ($type, $buffer) use ($output) {
108-
$output->write($buffer);
109-
}
110-
));
123+
$fullCommand = $codeceptionCommand . $testsDirectory . $testName . ' --verbose --steps';
124+
$this->returnCode = max($this->returnCode, $this->executeTestCommand($fullCommand, $output));
111125
}
112-
return $returnCode;
113126
}
114127

115128
/**
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
129+
* Run tests referenced in suites within suites' context.
130+
*
131+
* @param array $suitesConfig
132+
* @param OutputInterface $output
133+
* @return void
120134
*/
121-
private function resolveSuiteReferences($testConfigurationJson)
135+
private function runTestsInSuite(array $suitesConfig, OutputInterface $output)
122136
{
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-
}
137+
$codeceptionCommand = realpath(PROJECT_ROOT . '/vendor/bin/codecept') . ' run functional --verbose --steps ';
138+
//for tests in suites, run them as a group to run before and after block
139+
foreach (array_keys($suitesConfig) as $suite) {
140+
$fullCommand = $codeceptionCommand . " -g {$suite}";
141+
$this->returnCode = max($this->returnCode, $this->executeTestCommand($fullCommand, $output));
132142
}
133-
return array_merge($testArrayBuilder, $testsArray);
143+
}
144+
145+
/**
146+
* Runs the codeception test command and returns exit code
147+
*
148+
* @param string $command
149+
* @param OutputInterface $output
150+
* @return integer
151+
*
152+
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
153+
*/
154+
private function executeTestCommand(string $command, OutputInterface $output)
155+
{
156+
$process = new Process($command);
157+
$process->setWorkingDirectory(TESTS_BP);
158+
$process->setIdleTimeout(600);
159+
$process->setTimeout(0);
160+
return $process->run(function ($type, $buffer) use ($output) {
161+
$output->write($buffer);
162+
});
134163
}
135164
}

0 commit comments

Comments
 (0)