Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Samer_J's avatar

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

0 likes
15 replies
SaeedPrez's avatar

Did you run composer install? See if composer dump-autoload works..

2 likes
Samer_J's avatar

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?

Bonsi's avatar

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.

Samer_J's avatar

All good, I solved the problem by installing a fresh version of Laravel 5.4.

ftrillo's avatar

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.

23 likes
dlash02's avatar

That did not work... I had both namespace Tests; to tests\TestCase.php and the proper composer.json and still get the error

2 likes
kennedystephen's avatar

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.

Tray2's avatar

It's very simple.

  1. Install Laravel composer create-project laravel/laravel myniftyproject
  2. cd myniftyproject
  3. ./vendor/bin/phpunit
flayshon's avatar

@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!

nateritter's avatar

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.

1 like
tinker's avatar

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.

1 like

Please or to participate in this conversation.