laravel fatal error for TestCase not found
Asked Answered
B

11

21
http://localhost/laravel/app/tests/ExampleTest.php

if i run laravel it shows the following error

Fatal error: Class 'TestCase' not found in D:\xampp\htdocs\laravel\app\tests\ExampleTest.php on line 3 , any idea

Buffet answered 12/2, 2014 at 11:41 Comment(1)
I don't believe tests are meant to be ran that way. You are supposed to use PHPUnit through the command line.Gustatory
R
18

Run following command in your project's root directory

phpunit app/tests/

Update:

You can also run with the following command if phpunit is not installed on your machine. Which is also better to avoid version differences between team mates.

vendor/bin/phpunit

enter image description here

Riffle answered 28/2, 2014 at 22:7 Comment(1)
@ConnorLeech propably phpunit is not installed on your machine. You can try to run like this "vendor/bin/phpunit"Riffle
F
13

Check if your tests directory is listed on the classmap property of your composer.json file.

If not add it and run composer dump-autoload.

"autoload-dev": {
    "classmap": [
        "tests",
        "database/"
    ]
},
Freaky answered 1/5, 2017 at 20:4 Comment(1)
This saved me a lot of headache! I added tests to my classmap and then ran a composer dump. Fixed my problems. Thanks!Haemic
M
7

Had the same issue with PHPUnit, he would output always the same message : PHP Fatal error: Class 'Tests\TestCase' not found ... and indeed I think he had troubles to find that class so what you need to do is transform one line :

From this :

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class AnotherTest extends TestCase {
    public function testExample() {
        // Do something
    }
}

To this :

<?php

namespace Tests\Feature;

use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class AnotherTest extends TestCase {
    public function testExample() {
        // Do something
    }
}

Notice the first line in use and you should do this if there's any other files in the same case

Measly answered 25/5, 2019 at 10:1 Comment(1)
The issue here is that you're including the PHPUnit TestCase, and not the laravel one, so you're going to miss out on laravel specific featuresNore
I
6

In my case, i noticed that my root folder not contained the phpunit.xml file.

Solve it including the following code in phpunit.xml at the root folder:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false"
>
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./app/tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>
Inscrutable answered 1/3, 2016 at 11:39 Comment(0)
M
5

You haven't really given much information to go on here, but the specific problem is probably that you don't have an autoloader (thus the TestCase class that PHP is expecting isn't actually loaded. The underlying problem in turn is likely that you're trying to run ExampleTest.php directly on the terminal, whereas instead you must bootstrap a test environment correctly through Laravel and PHPUnit.

You should find a phpunit.xml file in the root of your Laravel install. This file tells PHPUnit how to bootstrap and start executing tests for your application, so you should just be able to run phpunit on the command line in the root of your application and it should work.

If that's not the issue, please do provide more detail for people to help with.

Matthieu answered 12/2, 2014 at 12:5 Comment(0)
O
3

I had same issue, you should run test from root folder, in your case 'http://localhost/laravel' and in terminal you just need to write phpunit, and test will be executed.

Odysseus answered 16/1, 2015 at 11:59 Comment(0)
H
2

I got the same error but none worked,

except for this one, this worked for me,

I tried reinstalling and upgrading my phpunit in Laravel 5.4,

  1. modify compose.json -> find these line and change the phpunit version

    "require-dev": { "phpunit/phpunit": "~6.4" },

  2. after editing, run on your cmd,

    composer update

  3. then run

    composer dump-autoload

  4. php artisan config:clear

  5. php artisan cache:clear

  6. finally, run phpunit

or run specific unit, phpunit --filter yourTestCaseClass

enter image description here

Hays answered 23/11, 2017 at 7:33 Comment(0)
H
1

The is caused by an outdated phpunit. If you using a globally installed phpunit, here is a link on how to update it https://mcmap.net/q/658994/-how-to-upgrade-phpunit-to-new-version . Or you can explicitly use the phpunit that was installed with the project.

Hebraize answered 27/5, 2018 at 5:45 Comment(0)
F
0

My laravel phpunit was working and then suddenly started giving me this error. Running composer install again solved it.

Fax answered 16/2, 2021 at 17:54 Comment(0)
R
0

Kindly check if the TestCase.php is exist in your test folder and if not create it

here is the content of TestCase.php

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
}

after that try to run the test hopefully this help thanks!

Rime answered 10/7 at 16:11 Comment(0)
I
-1

After wracking my brains over this for a very late hour, in the end it turned out I was completely missing the tests/TestCase.php file, for some mysterious reason. Creating the file with the following content, taken directly from the Laravel 9 GitHub repo, fixed the issue for me:

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
}
Importunate answered 3/3, 2023 at 4:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.