Phpunit: Class not found

Created on 9 Oct 2019  Â·  16Comments  Â·  Source: sebastianbergmann/phpunit

| Q | A
| --------------------| ---------------
| PHPUnit version | 8.4.0
| PHP version | 7.2.23
| Installation Method | Composer

Summary

Trying to start test, got 'Process finished with exit code 0' with trace:
Class 'Tests\Service\ClassTest' could not be found in '/tests/Service/ClassTest.php'

Previous Behavior

It works in 8.3.5

Current Behavior

It's broken it 8.4.0

How to reproduce

Run test

typbackward-compatibility typbug

Most helpful comment

Sounds like I need to move from ClassName.test.php to ClassNameTest.php. I find the former a lot clearer when scanning over large numbers of files, which is why I started doing it. But it's not that big-a-deal.

thanks for your help.

All 16 comments

Thank you for your report.

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.

Also please try with PHPUnit 8.4.1 and see if your problem still exists.

EDIT: Found the issue. The reason is that the test class name did not exactly match the file name in which the test class is written. This mismatch was allowed in earlier versions, but causes a class load error in 8.4.*.

Not an issue with PHPUnit really, although behavior has changed.

Original post below;


Same problem for me with 8.4.1, backed to 8.3.5 which works. Running PHP 7.3.5.

Only backtrace for now, will look into it later.

Uncaught PHPUnit\Runner\Exception: Class 'MyTest' could not be found in 'project/tests/MyTest.php'.

PHPUnit\Runner\StandardTestSuiteLoader->load(…) in project/include/deps/phpunit/phpunit/src/Runner/BaseTestRunner.php, line 141
PHPUnit\Runner\BaseTestRunner->loadSuiteClass(…) in project/include/deps/phpunit/phpunit/src/Runner/BaseTestRunner.php, line 101
PHPUnit\Runner\BaseTestRunner->getTest(…) in project/include/deps/phpunit/phpunit/src/TextUI/Command.php, line 177
PHPUnit\TextUI\Command->run(…) in project/include/deps/phpunit/phpunit/src/TextUI/Command.php, line 159
PHPUnit\TextUI\Command::main() in project/include/deps/phpunit/phpunit/phpunit, line 61

I have the same issue going from 8.3.5 => 8.4.1. I suffix my tests with ".test.php" and make use of <directory suffix=".test.php"> in phpunit.xml.

In 8.3.5 I could run a test directly with ./vendor/bin/phpunit tests/path/to/Class.test.php. In 8.4.1 this gives the following error:

PHP Fatal error:  Uncaught PHPUnit\Runner\Exception: Class 'Class.test.php' could not be found in '/project/path/tests/path/to/Class.test.php'. in /project/path/vendor/phpunit/phpunit/src/Runner/StandardTestSuiteLoader.php:69
Stack trace:
#0 /project/path/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(141): PHPUnit\Runner\StandardTestSuiteLoader->load('Class.t...', '/project/path/dir...')
#1 /project/path/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(101): PHPUnit\Runner\BaseTestRunner->loadSuiteClass('tests/client-lm...', '/project/path/dir...')
#2 /project/path/vendor/phpunit/phpunit/src/TextUI/Command.php(177): PHPUnit\Runner\BaseTestRunner->getTest('tests/client-lm...', '/project/path/dir...', Array)
#3 /project/path/vendor/phpunit/phpunit/src/TextUI/Command.php(159): PHPUnit\TextUI\Command->run(Array, true)
#4 /project/path/vendor/phpunit/phpunit/phpunit(61): PHPUnit\TextUI\ in /project/path/vendor/phpunit/phpunit/src/Runner/StandardTestSuiteLoader.php on line 69

Fatal error: Uncaught PHPUnit\Runner\Exception: Class 'Class.test.php' could not be found in '/project/path/tests/path/to/Class.test.php'. in /project/path/vendor/phpunit/phpunit/src/Runner/StandardTestSuiteLoader.php:69
Stack trace:
#0 /project/path/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(141): PHPUnit\Runner\StandardTestSuiteLoader->load('Class.t...', '/project/path/dir...')
#1 /project/path/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(101): PHPUnit\Runner\BaseTestRunner->loadSuiteClass('tests/client-lm...', '/project/path/dir...')
#2 /project/path/vendor/phpunit/phpunit/src/TextUI/Command.php(177): PHPUnit\Runner\BaseTestRunner->getTest('tests/client-lm...', '/project/path/dir...', Array)
#3 /project/path/vendor/phpunit/phpunit/src/TextUI/Command.php(159): PHPUnit\TextUI\Command->run(Array, true)
#4 /project/path/vendor/phpunit/phpunit/phpunit(61): PHPUnit\TextUI\ in /project/path/vendor/phpunit/phpunit/src/Runner/StandardTestSuiteLoader.php on line 69

CC @flow-control

Hey @sirn-se and @dmlogic,

could you provide me the class names in your files? I will then create a test for these and try to fix the regression.

/Flo

An example for a file tests/MyTest.test.php would be:

<?php

use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
    /**
     * @test
     */
    public function my_sample_test()
    {
        $this->assertTrue(true);
    }
}

and the testsuite would be defined in phpunit.xml as:

<testsuite name="sample">
    <directory suffix=".test.php">./tests</directory>
</testsuite>

I use this approach as I sometimes need php includes in the tests folders and don't want them to be considered tests

File tests/MyLoadTest.php

<?
class LoadTest extends PHPUnit\Framework\TestCase
{
    // Doesn't matter, will not be loaded to begin with
}

Obviously class and file name doesnt't match, but before it ran anyway. @flow-control

I fixed that in #3891 by restoring a few lines i removed in #3830

I like to add, that naming your test class FooTest while naming the file MyFooTest.php seems not intentionally supported (at least to my knowledge), but possible due to another feature.

If the runner can not find a class with the name of the file (minus suffix) it checks all classes loaded from the given test file for not being abstract and being a subclass of PHPUnit\Framework\TestCase. Therefore, not intentionally (at least to my knowledge) you can name your test class whatever you like ;-)

@flow-control Can you please send a pull request that reverts #3891 (after I have merged it)? I would like to schedule that for PHPUnit 9 as I agree that this should not be supported.

I fixed that in #3891

Thank you!

Therefore, not intentionally (at least to my knowledge) you can name your test class whatever you like

Understood

I would like to schedule that for PHPUnit 9 as I agree that this should not be supported

@sebastianbergmann does this mean I will be able to continue using a .test.php suffix for 8.x but not for 9.x?

Hey @dmlogic,

calling the test directly as you stated above with ./vendor/bin/phpunit tests/path/to/Class.test.php is at this moment only possible due to the loader checking on every class in that file. The suffix option and the --test-suffix command line switch at this moment are only used to find files if you are giving phpunit a directory and not a file. So calling ./vendor/bin/phpunit --test-suffix .test.php tests/path/to/ should do the trick, but then again this will execute all other *.test.php test files aswell.

Calling the test file directly as you did will not be possible when reverting this pull request in version 9. What's your opinion on that @sebastianbergmann?

/Flo

Sounds like I need to move from ClassName.test.php to ClassNameTest.php. I find the former a lot clearer when scanning over large numbers of files, which is why I started doing it. But it's not that big-a-deal.

thanks for your help.

is this described in the breaking changes?

I could not find it and we will have many test classes to rename

(especially in older tests before namespaces were introduced,

where for example we would have a class named Tests_PHPUnit_Lib_DatesTest in a DatesTest.php file)

(not a problem, but it would be nice to mention it in the breaking changes if it is not already the case)

Hey there :vulcan_salute:

this is stated in https://github.com/sebastianbergmann/phpunit/blob/9.1.0/ChangeLog-9.1.md at the bottom:

4105: Deprecate multiple test case classes in single file and test case class name differing from filename

Maybe this could help you prepare for PHPUnit 10: https://github.com/rectorphp/rector/blob/master/docs/rector_rules_overview.md#pseudonamespacetonamespacerecto

Hope I could help

/Flo

thanks a lot Flo, it did help.

(I looked at the 9.0 changelog on the website and tried to find more on master,

I did see a changelog for 8.5, 9.2 and 9.3, but not 9.1,

and did not think to look at the tags)

Was this page helpful?
0 / 5 - 0 ratings