Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support filtering using --filter, --exclude-filter, --group, and --exclude-group when listing tests using --list-tests and --list-tests-xml as well as listing groups with --list-groups #5720

Closed
sebastianbergmann opened this issue Mar 3, 2024 · 3 comments
Assignees
Labels
event/code-sprint/2024-03 PHPUnit Code Sprint: March 2024 feature/test-runner CLI test runner type/enhancement A new idea that should be implemented
Milestone

Comments

@sebastianbergmann
Copy link
Owner

No description provided.

@sebastianbergmann sebastianbergmann added type/enhancement A new idea that should be implemented feature/test-runner CLI test runner labels Mar 3, 2024
@sebastianbergmann sebastianbergmann added this to the PHPUnit 11.1 milestone Mar 3, 2024
@sebastianbergmann sebastianbergmann self-assigned this Mar 3, 2024
@sebastianbergmann
Copy link
Owner Author

After the preparation work that was already done in https://github.com/sebastianbergmann/phpunit/tree/issue-5720/support-filtering-when-listing-tests, this should be as simple as this:

diff --git a/src/TextUI/Command/Commands/ListTestsAsTextCommand.php b/src/TextUI/Command/Commands/ListTestsAsTextCommand.php
index de8d3bde9..3cb4dc252 100644
--- a/src/TextUI/Command/Commands/ListTestsAsTextCommand.php
+++ b/src/TextUI/Command/Commands/ListTestsAsTextCommand.php
@@ -15,6 +15,7 @@
 use PHPUnit\Framework\TestSuite;
 use PHPUnit\Runner\PhptTestCase;
 use PHPUnit\TextUI\Configuration\Configuration;
+use PHPUnit\TextUI\TestSuiteFilterProcessor;
 use RecursiveIteratorIterator;
 
 /**
@@ -35,6 +36,8 @@ public function execute(): Result
     {
         $buffer = 'Available test(s):' . PHP_EOL;
 
+        (new TestSuiteFilterProcessor)->process($this->configuration, $this->suite);
+
         foreach (new RecursiveIteratorIterator($this->suite) as $test) {
             if ($test instanceof TestCase) {
                 $name = sprintf(

Unfortunately, it is not:

$ ./phpunit --do-not-cache-result --no-configuration --filter testOne tests/end-to-end/_files/listing-tests-and-groups --list-tests
PHPUnit 11.1-g3968118c01 by Sebastian Bergmann and contributors.

An error occurred inside PHPUnit.

Message:  Too few arguments to function PHPUnit\Runner\Filter\NameFilterIterator::__construct(), 1 passed and exactly 2 expected
Location: /usr/local/src/phpunit/src/Runner/Filter/NameFilterIterator.php:44

#0 [internal function]: PHPUnit\Runner\Filter\NameFilterIterator->__construct()
#1 /usr/local/src/phpunit/src/TextUI/Command/Commands/ListTestsAsTextCommand.php(41): RecursiveFilterIterator->getChildren()
#2 /usr/local/src/phpunit/src/TextUI/Application.php(266): PHPUnit\TextUI\Command\ListTestsAsTextCommand->execute()
#3 /usr/local/src/phpunit/src/TextUI/Application.php(424): PHPUnit\TextUI\Application->execute()
#4 /usr/local/src/phpunit/src/TextUI/Application.php(111): PHPUnit\TextUI\Application->executeCommandsThatRequireTheTestSuite()
#5 /usr/local/src/phpunit/phpunit(104): PHPUnit\TextUI\Application->run()
#6 {main}

The TestSuiteFilterProcessor uses the Configuration to inject filters (using TestSuite::injectFilter()) into the TestSuite. This "has always worked fine" for running tests, which is initiated by TestSuite::run(). This method uses foreach ($this as $test) to iterate over the tests of a test suite. This causes TestSuite::getIterator() to be called (as TestSuite implements IteratorAggregate), which in turn creates an iterator chain based on the injected filters. This does not happen when foreach (new RecursiveIteratorIterator($this->suite) as $test) is performed in the ListTestsAsTextCommand.

When we change the command like so ...

diff --git a/src/TextUI/Command/Commands/ListTestsAsTextCommand.php b/src/TextUI/Command/Commands/ListTestsAsTextCommand.php
index de8d3bde9..64bb80ad4 100644
--- a/src/TextUI/Command/Commands/ListTestsAsTextCommand.php
+++ b/src/TextUI/Command/Commands/ListTestsAsTextCommand.php
@@ -15,6 +15,7 @@
 use PHPUnit\Framework\TestSuite;
 use PHPUnit\Runner\PhptTestCase;
 use PHPUnit\TextUI\Configuration\Configuration;
+use PHPUnit\TextUI\TestSuiteFilterProcessor;
 use RecursiveIteratorIterator;
 
 /**
@@ -35,7 +36,9 @@ public function execute(): Result
     {
         $buffer = 'Available test(s):' . PHP_EOL;
 
-        foreach (new RecursiveIteratorIterator($this->suite) as $test) {
+        (new TestSuiteFilterProcessor)->process($this->configuration, $this->suite);
+
+        foreach ($this->suite as $test) {
             if ($test instanceof TestCase) {
                 $name = sprintf(
                     '%s::%s',

... then we do not run into the error shown above, but it also does not work:

$ ./phpunit --do-not-cache-result --no-configuration --filter testOne tests/end-to-end/_files/listing-tests-and-groups --list-tests
PHPUnit 11.1-g3968118c01 by Sebastian Bergmann and contributors.

Available test(s):

I am leaving the above information here for "future me" or anybody else who wants to debug this mystery. As for myself, I think we would be better served to refactor how filtering is performed. This is what I will look into next ...

@sebastianbergmann
Copy link
Owner Author

The issue-5720/support-filtering-when-listing-tests branch has some preparatory refactoring as well as tests for this feature.

On the filter-refactoring branch, I have started to explore how filtering could be refactored.

@staabm
Copy link
Contributor

staabm commented Mar 8, 2024

thank you!

@sebastianbergmann sebastianbergmann changed the title Support filtering using --filter, --exclude-filter, --group, and --exclude-group when listing tests using --list-tests and --list-tests-xml Support filtering using --filter, --exclude-filter, --group, and --exclude-group when listing tests using --list-tests and --list-tests-xml as well as listing groups with --list-groups Mar 8, 2024
@sebastianbergmann sebastianbergmann added the event/code-sprint/2024-03 PHPUnit Code Sprint: March 2024 label Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event/code-sprint/2024-03 PHPUnit Code Sprint: March 2024 feature/test-runner CLI test runner type/enhancement A new idea that should be implemented
Projects
None yet
Development

No branches or pull requests

2 participants