diff --git a/CHANGELOG.md b/CHANGELOG.md index b4d56b49..385e9d0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ## Unreleased ### Added - Check if browser given to `run-tests` command is supported (this helps avoiding typos, interchange of browser and environment etc.). +- Option `--filter` which allows filtering tests/testcases by name (@ziizii) ### Changed - The `logs/results.xml` could now be also accessed locally (the XSLT is now embedded right into the file, so we don't encounter same-origin policy problems). diff --git a/src-tests/Process/ProcessSetCreatorTest.php b/src-tests/Process/ProcessSetCreatorTest.php index a331869e..f12eaa4f 100644 --- a/src-tests/Process/ProcessSetCreatorTest.php +++ b/src-tests/Process/ProcessSetCreatorTest.php @@ -83,13 +83,13 @@ public function testShouldCreateEmptyProcessSetIfNoFilesGiven() public function testShouldCreateProcessSetFromGivenFiles() { - $processSet = $this->creator->createFromFiles($this->findDummyTests(), [], []); + $processSet = $this->creator->createFromFiles($this->findDummyTests(), [], [], ''); $this->assertQueuedTests([self::NAME_DUMMY_TEST, self::NAME_BAR_TEST, self::NAME_FOO_TEST], $processSet); // Test properties of DummyTest - /** @var Process $dummyTestProcess */ $processes = $processSet->get(ProcessSet::PROCESS_STATUS_QUEUED); + /** @var Process $dummyTestProcess */ $dummyTestProcess = $processes[self::NAME_DUMMY_TEST]->process; $testCommand = $dummyTestProcess->getCommandLine(); $testEnv = $dummyTestProcess->getEnv(); @@ -97,6 +97,7 @@ public function testShouldCreateProcessSetFromGivenFiles() $this->assertContains('phpunit', $testCommand); $this->assertContains('--log-junit=logs/Lmc-Steward-Process-Fixtures-DummyTests-DummyTest.xml', $testCommand); $this->assertNotContains('--colors', $testCommand); // Decorated output is disabled in setUp() + $this->assertNotContains('--filter', $testCommand); $this->assertRegExp('/--configuration=.*\/src\/phpunit\.xml/', $testCommand); // Check defaults were passed to the Processes @@ -150,6 +151,21 @@ public function testShouldAddTestsOfGivenGroupsButExcludeFromThemThoseOfExcluded $this->assertRegExp('/Excluding testcase file .*\/GroupBarTest\.php with group bar/', $output); } + public function testShouldPassFilterOptionToPhpunitProcess() + { + $processSet = $this->creator->createFromFiles($this->findDummyTests(), [], [], 'testCase::testName'); + + $processes = $processSet->get(ProcessSet::PROCESS_STATUS_QUEUED); + /** @var Process $dummyTestProcess */ + $dummyTestProcess = $processes[self::NAME_DUMMY_TEST]->process; + $testCommand = $dummyTestProcess->getCommandLine(); + $output = $this->bufferedOutput->fetch(); + + $this->assertContains('Filtering testcases:', $output); + $this->assertContains('by testcase/test name: testCase::testName', $output); + $this->assertContains('--filter=testCase::testName', $testCommand); + } + public function testShouldPropagateCustomOptionsIntoProcess() { $this->input = new StringInput( diff --git a/src/Console/Command/RunTestsCommand.php b/src/Console/Command/RunTestsCommand.php index 9c479d8e..b625d4c8 100644 --- a/src/Console/Command/RunTestsCommand.php +++ b/src/Console/Command/RunTestsCommand.php @@ -46,6 +46,7 @@ class RunTestsCommand extends Command const OPTION_PATTERN = 'pattern'; const OPTION_GROUP = 'group'; const OPTION_EXCLUDE_GROUP = 'exclude-group'; + const OPTION_FILTER = 'filter'; const OPTION_PUBLISH_RESULTS = 'publish-results'; /** @@ -127,6 +128,12 @@ protected function configure() InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Exclude testcases with specified @group from being run' ) + ->addOption( + self::OPTION_FILTER, + null, + InputOption::VALUE_REQUIRED, + 'Run only testcases/tests with name matching this filter' + ) ->addOption( self::OPTION_PUBLISH_RESULTS, null, @@ -246,7 +253,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $processSet = $processSetCreator->createFromFiles( $files, $input->getOption(self::OPTION_GROUP), - $input->getOption(self::OPTION_EXCLUDE_GROUP) + $input->getOption(self::OPTION_EXCLUDE_GROUP), + $input->getOption(self::OPTION_FILTER) ); if (!count($processSet)) { diff --git a/src/Process/ProcessSetCreator.php b/src/Process/ProcessSetCreator.php index d0bd8a6d..e5ded57b 100644 --- a/src/Process/ProcessSetCreator.php +++ b/src/Process/ProcessSetCreator.php @@ -54,11 +54,12 @@ public function __construct( * @param Finder $files * @param array $groups Groups to be run * @param array $excludeGroups Groups to be excluded + * @param string $filter filter test cases by name * @return ProcessSet */ - public function createFromFiles(Finder $files, array $groups = null, array $excludeGroups = null) + public function createFromFiles(Finder $files, array $groups = null, array $excludeGroups = null, $filter = null) { - if ($groups || $excludeGroups) { + if ($groups || $excludeGroups || $filter) { $this->output->writeln('Filtering testcases:'); } if ($groups) { @@ -67,6 +68,9 @@ public function createFromFiles(Finder $files, array $groups = null, array $excl if ($excludeGroups) { $this->output->writeln(sprintf(' - excluding group(s): %s', implode(', ', $excludeGroups))); } + if ($filter) { + $this->output->writeln(sprintf(' - by testcase/test name: %s', $filter)); + } $processSet = $this->getProcessSet(); @@ -126,6 +130,10 @@ public function createFromFiles(Finder $files, array $groups = null, array $excl '--configuration=' . realpath(__DIR__ . '/../phpunit.xml'), ]; + if ($filter) { + $phpunitArgs[] = '--filter=' . $filter; + } + // If ANSI output is enabled, turn on colors in PHPUnit if ($this->output->isDecorated()) { $phpunitArgs[] = '--colors=always';