Skip to content

Commit 093bb5e

Browse files
authored
Merge pull request #127 from magento-commerce/MQE-2677-2.x-develop
[2.x-develop] MQE-2677: Add filter for groups
2 parents ca55fac + f481351 commit 093bb5e

File tree

5 files changed

+220
-3
lines changed

5 files changed

+220
-3
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/Util/TestGeneratorTest.php

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function testAllowSkipped()
116116
*
117117
* @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
118118
*/
119-
public function testFilter()
119+
public function testSeverityFilter()
120120
{
121121
// Mock filters for TestGenerator
122122
AspectMock::double(
@@ -160,4 +160,104 @@ public function testFilter()
160160
$this->assertArrayHasKey('test1Cest', $generatedTests);
161161
$this->assertArrayNotHasKey('test2Cest', $generatedTests);
162162
}
163+
164+
/**
165+
* Tests that TestGenerator createAllTestFiles correctly filters based on group included.
166+
*
167+
* @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
168+
*/
169+
public function testIncludeGroupFilter()
170+
{
171+
// Mock filters for TestGenerator
172+
AspectMock::double(
173+
MftfApplicationConfig::class,
174+
['getFilterList' => new FilterList(['includeGroup' => ["someGroupValue"]])]
175+
);
176+
177+
$actionInput = 'fakeInput';
178+
$actionObject = new ActionObject('fakeAction', 'comment', [
179+
'userInput' => $actionInput
180+
]);
181+
182+
$annotation1 = ['group' => ['someGroupValue']];
183+
$annotation2 = ['group' => ['someOtherGroupValue']];
184+
$test1 = new TestObject(
185+
"test1",
186+
["fakeAction" => $actionObject],
187+
$annotation1,
188+
[],
189+
"filename"
190+
);
191+
$test2 = new TestObject(
192+
"test2",
193+
["fakeAction" => $actionObject],
194+
$annotation2,
195+
[],
196+
"filename"
197+
);
198+
AspectMock::double(TestGenerator::class, ['loadAllTestObjects' => ["sampleTest" => $test1, "test2" => $test2]]);
199+
200+
// Mock createCestFile to return name of tests that testGenerator tried to create
201+
$generatedTests = [];
202+
AspectMock::double(TestGenerator::class, ['createCestFile' => function ($arg1, $arg2) use (&$generatedTests) {
203+
$generatedTests[$arg2] = true;
204+
}]);
205+
206+
$testGeneratorObject = TestGenerator::getInstance("", ["sampleTest" => $test1, "test2" => $test2]);
207+
$testGeneratorObject->createAllTestFiles(null, []);
208+
209+
// Ensure Test1 was Generated but not Test 2
210+
$this->assertArrayHasKey('test1Cest', $generatedTests);
211+
$this->assertArrayNotHasKey('test2Cest', $generatedTests);
212+
}
213+
214+
/**
215+
* Tests that TestGenerator createAllTestFiles correctly filters based on group not included.
216+
*
217+
* @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
218+
*/
219+
public function testExcludeGroupFilter()
220+
{
221+
// Mock filters for TestGenerator
222+
AspectMock::double(
223+
MftfApplicationConfig::class,
224+
['getFilterList' => new FilterList(['excludeGroup' => ['someGroupValue']])]
225+
);
226+
227+
$actionInput = 'fakeInput';
228+
$actionObject = new ActionObject('fakeAction', 'comment', [
229+
'userInput' => $actionInput
230+
]);
231+
232+
$annotation1 = ['group' => ['someGroupValue']];
233+
$annotation2 = ['group' => ['someOtherGroupValue']];
234+
$test1 = new TestObject(
235+
"test1",
236+
["fakeAction" => $actionObject],
237+
$annotation1,
238+
[],
239+
"filename"
240+
);
241+
$test2 = new TestObject(
242+
"test2",
243+
["fakeAction" => $actionObject],
244+
$annotation2,
245+
[],
246+
"filename"
247+
);
248+
AspectMock::double(TestGenerator::class, ['loadAllTestObjects' => ["sampleTest" => $test1, "test2" => $test2]]);
249+
250+
// Mock createCestFile to return name of tests that testGenerator tried to create
251+
$generatedTests = [];
252+
AspectMock::double(TestGenerator::class, ['createCestFile' => function ($arg1, $arg2) use (&$generatedTests) {
253+
$generatedTests[$arg2] = true;
254+
}]);
255+
256+
$testGeneratorObject = TestGenerator::getInstance("", ["sampleTest" => $test1, "test2" => $test2]);
257+
$testGeneratorObject->createAllTestFiles(null, []);
258+
259+
// Ensure Test2 was Generated but not Test 1
260+
$this->assertArrayNotHasKey('test1Cest', $generatedTests);
261+
$this->assertArrayHasKey('test2Cest', $generatedTests);
262+
}
163263
}

docs/commands/mftf.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ vendor/bin/mftf generate:tests [option] [<test name>] [<test name>] [--remove]
159159
| Option | Description|
160160
| ---| --- |
161161
| `--config=[<default> or <singleRun> or <parallel>]` | Creates a single manifest file with a list of all tests. The default location is `tests/functional/Magento/FunctionalTest/_generated/testManifest.txt`.<br/> You can split the list into multiple groups using `--config=parallel`; the groups will be generated in `_generated/groups/` like `_generated/groups/group1.txt, group2.txt, ...`.<br/> Available values: `default` (default), `singleRun`(same as `default`), and `parallel`.<br/> Example: `generate:tests --config=parallel`. |
162-
| `--filter` | Option to filter tests to be generated.<br/>Template: '&lt;filterName&gt;:&lt;filterValue&gt;'.<br/>Existing filter types: severity.<br/>Existing severity values: BLOCKER, CRITICAL, MAJOR, AVERAGE, MINOR.<br/>Example: --filter=severity:CRITICAL|
162+
| `--filter` | Option to filter tests to be generated.<br/>Template: '&lt;filterName&gt;:&lt;filterValue&gt;'.<br/>Existing filter types: severity, includeGroup, excludeGroup.<br/>Existing severity values: BLOCKER, CRITICAL, MAJOR, AVERAGE, MINOR.<br/>Example: `vendor/bin/mftf generate:tests --filter=severity:CRITICAL --filter=severity:BLOCKER --filter=includeGroup:customer`|
163163
| `--force` | Forces test generation, regardless of the module merge order defined in the Magento instance. Example: `generate:tests --force`. |
164164
| `-i,--time` | Set time in minutes to determine the group size when `--config=parallel` is used. The __default value__ is `10`. Example: `generate:tests --config=parallel --time=15`|
165165
| `--tests` | Defines the test configuration as a JSON string.|

src/Magento/FunctionalTestingFramework/Console/GenerateTestsCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ protected function configure()
5959
. '<info>Template:</info> <filterName>:<filterValue>' . PHP_EOL
6060
. '<info>Existing filter types:</info> severity.' . PHP_EOL
6161
. '<info>Existing severity values:</info> BLOCKER, CRITICAL, MAJOR, AVERAGE, MINOR.' . PHP_EOL
62-
. '<info>Example:</info> --filter=severity:CRITICAL' . PHP_EOL
62+
. '<info>Example:</info> --filter=severity:CRITICAL'
63+
. ' --filter=includeGroup:customer --filter=excludeGroup:customerAnalytics' . PHP_EOL
6364
);
6465

6566
parent::configure();
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\FunctionalTestingFramework\Filter\Test;
10+
11+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
12+
use Magento\FunctionalTestingFramework\Filter\FilterInterface;
13+
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
14+
15+
/**
16+
* Class ExcludeGroup
17+
*/
18+
class ExcludeGroup implements FilterInterface
19+
{
20+
const ANNOTATION_TAG = 'group';
21+
22+
/**
23+
* @var array
24+
*/
25+
private $filterValues = [];
26+
27+
/**
28+
* Group constructor.
29+
*
30+
* @param array $filterValues
31+
* @throws TestFrameworkException
32+
*/
33+
public function __construct(array $filterValues = [])
34+
{
35+
$this->filterValues = $filterValues;
36+
}
37+
38+
/**
39+
* Filter tests by group.
40+
*
41+
* @param TestObject[] $tests
42+
* @return void
43+
*/
44+
public function filter(array &$tests)
45+
{
46+
if ($this->filterValues === []) {
47+
return;
48+
}
49+
/** @var TestObject $test */
50+
foreach ($tests as $testName => $test) {
51+
$groups = $test->getAnnotationByName(self::ANNOTATION_TAG);
52+
$testExcludeGroup = !empty(array_intersect($groups, $this->filterValues));
53+
if ($testExcludeGroup) {
54+
unset($tests[$testName]);
55+
}
56+
}
57+
}
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\FunctionalTestingFramework\Filter\Test;
10+
11+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
12+
use Magento\FunctionalTestingFramework\Filter\FilterInterface;
13+
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
14+
15+
/**
16+
* Class IncludeGroup
17+
*/
18+
class IncludeGroup implements FilterInterface
19+
{
20+
const ANNOTATION_TAG = 'group';
21+
22+
/**
23+
* @var array
24+
*/
25+
private $filterValues = [];
26+
27+
/**
28+
* Group constructor.
29+
*
30+
* @param array $filterValues
31+
* @throws TestFrameworkException
32+
*/
33+
public function __construct(array $filterValues = [])
34+
{
35+
$this->filterValues = $filterValues;
36+
}
37+
38+
/**
39+
* Filter tests by group.
40+
*
41+
* @param TestObject[] $tests
42+
* @return void
43+
*/
44+
public function filter(array &$tests)
45+
{
46+
if ($this->filterValues === []) {
47+
return;
48+
}
49+
/** @var TestObject $test */
50+
foreach ($tests as $testName => $test) {
51+
$groups = $test->getAnnotationByName(self::ANNOTATION_TAG);
52+
$testIncludeGroup = empty(array_intersect($groups, $this->filterValues));
53+
if ($testIncludeGroup) {
54+
unset($tests[$testName]);
55+
}
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)