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

Abstract *Test classes cannot be found #4979

Closed
derrabus opened this issue Jun 2, 2022 · 5 comments
Closed

Abstract *Test classes cannot be found #4979

derrabus opened this issue Jun 2, 2022 · 5 comments
Assignees
Labels
feature/test-runner CLI test runner type/bug Something is broken
Milestone

Comments

@derrabus
Copy link
Contributor

derrabus commented Jun 2, 2022

Q A
PHPUnit version 10.0-dev
PHP version 8.1.6
Installation Method Composer

Summary

I wanted to try out PHPUnit 10-dev on a project that has a green and (almost) deprecation-free test suite running on PHPUnit 9.5. In that project, there are several abstract classes following the naming convention AbstractSomethingTest. Then there are non-abstract child classes which are actually executable test cases.

When running PHPUnit 10, it crashes on the first of those abstract classes, claiming that it could not find the class:

PHP Fatal error:  Uncaught PHPUnit\Runner\Exception: Class 'MyApp\Tests\SomeFeature\AbstractSomethingTest' could not be found in '/path/to/project/tests/SomeFeature/AbstractSomethingTest.php'. in /path/to/project/vendor/phpunit/phpunit/src/Runner/TestSuiteLoader.php:147
Stack trace:
#0 /path/to/project/vendor/phpunit/phpunit/src/Runner/TestSuiteLoader.php(125): PHPUnit\Runner\TestSuiteLoader->exceptionFor('MyApp\\Tests\\...', '/path/to/projec...')
#1 /path/to/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php(295): PHPUnit\Runner\TestSuiteLoader->load('/path/to/projec...')
#2 /path/to/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php(307): PHPUnit\Framework\TestSuite->addTestFile('/path/to/projec...')
#3 /path/to/project/vendor/phpunit/phpunit/src/TextUI/Configuration/Xml/TestSuiteMapper.php(74): PHPUnit\Framework\TestSuite->addTestFiles(Array)
#4 /path/to/project/vendor/phpunit/phpunit/src/TextUI/Configuration/TestSuiteBuilder.php(56): PHPUnit\TextUI\XmlConfiguration\TestSuiteMapper->map(Object(PHPUnit\TextUI\XmlConfiguration\TestSuiteCollection), '', '')
#5 /path/to/project/vendor/phpunit/phpunit/src/TextUI/Application.php(243): PHPUnit\TextUI\Configuration\TestSuiteBuilder->build(Object(PHPUnit\TextUI\CliArguments\Configuration), Object(PHPUnit\TextUI\XmlConfiguration\LoadedFromFileConfiguration))
#6 /path/to/project/vendor/phpunit/phpunit/src/TextUI/Application.php(95): PHPUnit\TextUI\Application->handleArguments(Array)
#7 /path/to/project/vendor/phpunit/phpunit/src/TextUI/Application.php(78): PHPUnit\TextUI\Application->run(Array, true)
#8 /path/to/project/vendor/phpunit/phpunit/phpunit(90): PHPUnit\TextUI\Application::main()
#9 {main}

Stacktrace redacted to remove references to the actual project. I could provide a minimal reproducer if necessary.

This error message is pretty odd because the class PHPUnit complains about is defined exactly in the file mentioned in the exception message.

If I rename that one class from AbstractSomethingTest to SomethingTestCase and run PHPUnit again, it crashes on the next class following that pattern. If I rename all of the affected classes, PHPUnit runs fine.

Apparently, PHPUnit does not like abstract classes anymore that use the Test suffix. I wasn't sure if this was an intended breaking change or not, but I could not find documentation on this. Renaming those classes wouldn't be a big deal in my case (it would only affect 10 or so classes). But if this is really an intentional change, I believe something must be done about the error message at least because that message is highly misleading.

@derrabus derrabus added the type/bug Something is broken label Jun 2, 2022
@sebastianbergmann
Copy link
Owner

Apparently, PHPUnit does not like abstract classes anymore that use the Test suffix.

Correct.

I believe something must be done about the error message at least because that message is highly misleading.

Agreed.

@sebastianbergmann sebastianbergmann added this to the PHPUnit 10.0 milestone Jun 2, 2022
@sebastianbergmann sebastianbergmann added the feature/test-runner CLI test runner label Jun 2, 2022
@sebastianbergmann sebastianbergmann changed the title PHPUnit 10 claims that it cannot find abstract *Test classes Abstract *Test classes cannot be found Sep 8, 2022
@sebastianbergmann sebastianbergmann self-assigned this Sep 23, 2022
@IonBazan
Copy link
Contributor

IonBazan commented Dec 1, 2022

I think this should produce a deprecation warning in 9.5 branch so that users are informed about this change in advance.
I tried to add it in

if (!$class->isAbstract()) {
but it produced many false-positives as this part is executed for each child class too.

@derrabus
Copy link
Contributor Author

Thank you for fixing this issue!

@kevinpapst
Copy link

Any chance you @sebastianbergmann can share why that change was introduced?

I assume there are many projects following this obvious inheritance and naming scheme. I find it weird that, after years of compatibility, the test framework now forces me to rename them.

Would be "less painful" 😁 if we would understand what the technical reason for that change was. The original issue #5132 does not share any insights.

@k00ni
Copy link

k00ni commented Mar 9, 2023

One note: If you have files (with Test in the name) inside the tests folder, which do non-PHPUnit related testing, it will also complain with a warning.

k00ni added a commit to smalot/pdfparser that referenced this issue Mar 9, 2023
Since version 10, PHPUnit complains about:

1) Your XML configuration validates against a deprecated schema. Migrate
your XML configuration using "--migrate-configuration"!

2) Class AltAutoloadTest cannot be found in
D:\a\pdfparser\pdfparser\tests\AltAutoloadTest.php

3) Class Tests\Smalot\PdfParser\Performance\Test\AbstractPerformanceTest
cannot be found in
D:\a\pdfparser\pdfparser\tests\Performance\Test\AbstractPerformanceTest.php

4) Class
Tests\Smalot\PdfParser\Performance\Test\DocumentDictionaryCacheTest
cannot be found in
D:\a\pdfparser\pdfparser\tests\Performance\Test\DocumentDictionaryCacheTest.php

The first one is easy to solve.

But the other 3 are confusing, because PHPUnit 9 did not complain at
all. Reason for these warnings is that since PHPUnit v10, it doesn't
like abstract Test classes or files (containing Test in the name), which
contain no class at all.

Source: sebastianbergmann/phpunit#4979

To mitigate these warnings and to make maintenance easier in the future,
I reorganized our test files and put them in different folders. PHPUnit
tests are separated from our other tests.
k00ni added a commit to smalot/pdfparser that referenced this issue Mar 10, 2023
)

* Fixes failing tests; reorganized test files (PHPUnit 10.x related)

Since version 10, PHPUnit complains about:

1) Your XML configuration validates against a deprecated schema. Migrate
your XML configuration using "--migrate-configuration"!

2) Class AltAutoloadTest cannot be found in
D:\a\pdfparser\pdfparser\tests\AltAutoloadTest.php

3) Class Tests\Smalot\PdfParser\Performance\Test\AbstractPerformanceTest
cannot be found in
D:\a\pdfparser\pdfparser\tests\Performance\Test\AbstractPerformanceTest.php

4) Class
Tests\Smalot\PdfParser\Performance\Test\DocumentDictionaryCacheTest
cannot be found in
D:\a\pdfparser\pdfparser\tests\Performance\Test\DocumentDictionaryCacheTest.php

The first one is easy to solve.

But the other 3 are confusing, because PHPUnit 9 did not complain at
all. Reason for these warnings is that since PHPUnit v10, it doesn't
like abstract Test classes or files (containing Test in the name), which
contain no class at all.

Source: sebastianbergmann/phpunit#4979

To mitigate these warnings and to make maintenance easier in the future,
I reorganized our test files and put them in different folders. PHPUnit
tests are separated from our other tests.

* fixed alt autoloading

* reverted name change for AbstractPerformanceTest

* migrated Windows-related PHPUnit config file

* dev-tools: restrict PHPUnit version in composer.json

* Update dev-tools/composer.json

Co-authored-by: Jérémy Benoist <j0k3r@users.noreply.github.com>

---------

Co-authored-by: Jérémy Benoist <j0k3r@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/test-runner CLI test runner type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

5 participants