Skip to content

Commit

Permalink
Merge pull request #20 from OndraM/filter-option
Browse files Browse the repository at this point in the history
Filter option to filter test/testcases by name
  • Loading branch information
OndraM committed Jun 4, 2015
2 parents 1f505c0 + cb34cee commit d1359ae
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
20 changes: 18 additions & 2 deletions src-tests/Process/ProcessSetCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,21 @@ 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();

$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
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 9 additions & 1 deletion src/Console/Command/RunTestsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)) {
Expand Down
12 changes: 10 additions & 2 deletions src/Process/ProcessSetCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();

Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit d1359ae

Please sign in to comment.