Debug School

rakesh kumar
rakesh kumar

Posted on

PHPUnit class TestCase not found

Refer1
Refer2
Refer3
Refer4
Refer5
phpunit-9
phpunit

Error

Fatal error: Class 'PHPUnit\Framework\TestCase' not found in C:\xampp\htdocs\daily-news\EasyRSA\test\KeyPairTest.php on line 6
Enter fullscreen mode Exit fullscreen mode

I am working on creating a PHP library and want to start writing tests. I am getting an error Fatal error: Class 'PHPUnit\Framework\TestCase' not found.

My project structure is: in my main directory I have composer.json, a src/ directory with all my classes, a tests/ directory with unit/ and acceptance/ subdirectories. The tests I am trying to run are in the the unit/ directory. I am using the command line interface to run the test so the error happens when running phpunit tests/unit/testMyClass.php

testMyClass.php looks like:

<?php
require 'vendor/autoload.php';
use PHPUnit\Framework\TestCase;

class MyClassTest extends TestCase {
    public function testCreateMyClass() {
        // Tests are written here
    }
}
?>
Enter fullscreen mode Exit fullscreen mode

My composer.json is:

{
    "require-dev": {
        "phpunit/phpunit": "4.8.*"
    }
    "autoload": {
        "classmap": [
            "src/"
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Solution

case 1:run composer install

case 2:

I solved the problem with newer version of PHPUnit:

wget https://phar.phpunit.de/phpunit-6.0.phar
php phpunit-6.0.phar -c app/
Enter fullscreen mode Exit fullscreen mode

case 3:

I solve my problem with:

extends PHPUnit\Framework\TestCase
Enter fullscreen mode Exit fullscreen mode
class MyClassTest extends PHPUnit\Framework\TestCase {
     public function testCreateMyClass() {
        // Tests are written here
     }
}
Enter fullscreen mode Exit fullscreen mode

or

class MyClassTest extends PHPUnit_Framework_TestCase {
     public function testCreateMyClass() {
        // Tests are written here
     }
}
Enter fullscreen mode Exit fullscreen mode

case 4:

You appear to have one, or both of two problems.

Not using Composer, This will also provide a vendor/autoload.php file, that will automatically pull in any files that have been included via the composer.json file.
The latest version of PHPunit (v>6.0) no longer uses the long class names. the still-supported v5+ does though.
The recommended mechanism to install PHPunit 5.7 is:

composer require --dev phpunit/phpunit:^5.7
Enter fullscreen mode Exit fullscreen mode

Alternatively, it can be downloaded as a single phpunit.phar that can be run much as any other PHP script.

case 5:
php-fatal-error-class-phpunit-framework-testcase-not-found-with-phpunit-6-and

Just in case you are facing PHPUnit\Framework\TestCase not found in PhpStorm,

run php bin/phpunit at root of project
Enter fullscreen mode Exit fullscreen mode

, it will download all needed classes and PhpStorm error should gone
case 6:
case 7:

Top comments (0)