Did you run composer install? See if composer dump-autoload works..
Class 'Tests/TestCase' not found when attempting to run test
Can't find a solution to this anywhere.
I've simply followed along with the Laravel 5.4 docs in the "Testing: Getting Started" section. I created a new test using "make:test" and then executed "phpunit" from my terminal. This is the error message I get:
PHP Fatal error: Class 'Tests\TestCase' not found in /home/vagrant/Projects/mgn/tests/Feature/Test.php on line 10
This isn't a fresh project, I've been working on it for about two months now and have just now decided to start using the Test functionality. Running composer dump-autoload gives me a "Generating autoload files" message so it seems to work as intended.
My project was originally 5.3, so I followed the upgrade guide. I made all the necessary changes under "Updating Dependencies" and "Testing" and I'm still getting this error?
Testfiles were not namespaced before Laravel 5.4. What's the namespace of your tests/TestCase.php file?
To make diagnosing the issue here please include the full contents of your tests/TestCase.php, the new test and the "autoload" section of your composer.json.
All good, I solved the problem by installing a fresh version of Laravel 5.4.
I know this is a bit old. But If anyone comes across the same problem and doesn't want to install a fresh version of Laravel, this might help.
If you're working in a project that started in a previous version of Laravel and was then updated, the test files in your project might be outdated when you decide to start running some tests.
If your TestCase class doesn't have a namespace then you probably have the same problem I did.
Add namespace Tests; to tests\TestCase.php.
If the file contains any reference to a class in another namespace, make sure it starts with a leading slash.
Then you want to check your composer.json autoload configuration. It should contain this:
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
}
Instead of "classmap": ["tests/TestCase.php"]
Run composer dump-autoload and It should be fixed.
That did not work... I had both namespace Tests; to tests\TestCase.php and the proper composer.json and still get the error
Ugh. I've had so much struggle getting tests to run, where the test class has a depenedency on another class within the tests folder. Why is this so hard. No amount of composer dump-autoload helps. No amount of updating composer.json helps. No amount of namespacing helps. Still have NOT figured this out.
It's very simple.
- Install Laravel
composer create-project laravel/laravel myniftyproject -
cd myniftyproject -
./vendor/bin/phpunit
@ftrillo Your solution is really useful for when you want to separate the testing directories into Feature/Unit tests in Lumen just like it is in Laravel. Helped me a lot...thanks!
I had this issue and while @ftrillo response did help me realize that I probably had an outdated version of the test file, it was solved for me by copying the contents of TestCase.php from Github: https://github.com/laravel/laravel/blob/master/tests/TestCase.php
@Blindacme this worked for me!
I know this is a super old post, but I ran into this problem lately and realized the phpunit I had installed via brew was being used instead of the ./vendor/bin/phpunit
Even though they were the same version, only the ./vendor/bin/phpunit one worked.
Start with making sure you're calling the right phpunit executable if you run into this issue in 2023.
If anyone is also having a problem like this in 2025, I just added a new object
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
in the composer.json and executed composer dump-autoload, hope this helps.
@tinker Even though that section already exists in Laravel’s composer.json file? And has done for years?
Oh, sorry, I didn't know this thread was not part of the PHP For Beginners that I'm currently following.
Because there's also a problem I've encountered there where PestPHP is not working properly after executing ./vendor/bin/pest.
Please or to participate in this conversation.