Derek Spaulding
Derek Spaulding

Reputation: 203

PHPUnit class TestCase not found

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
    }
}
?>

My composer.json is:

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

Upvotes: 20

Views: 51194

Answers (5)

Alex
Alex

Reputation: 76

make sure the "phpunit.xml" file exists in the project root. In this file there must be a "phpunit" tag with a "bootstrap = "vendor / autoload.php" attribute.
Something like :

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
     bootstrap="vendor/autoload.php"
     colors="true">

In my case it solved the problem.

Upvotes: 1

R Sun
R Sun

Reputation: 1669

You need to run php bin/phpunit at root of project, it will download all needed classes and PhpStorm error should gone

Upvotes: -1

user11972673
user11972673

Reputation:

I solve my problem with:

extends PHPUnit\Framework\TestCase

Upvotes: 0

mwatzer
mwatzer

Reputation: 864

I had the same problem and I solved it by extending my test class from the PHPUnit_Framework_TestCase class instead of using the namespace PHPUnit\Framework\TestCase. After rebuilding your project-structure it worked fine for me.

tests/unit/testMyClass.php

<?php
require './vendor/autoload.php';

class MyClassTest extends PHPUnit_Framework_TestCase {
     public function testCreateMyClass() {
        // Tests are written here
     }
}
?>

composer.json

{
   "name": "root/project",
   "authors": [
      {
           "name": "watzerm",
           "email": "[email protected]"
      }
   ],
   "require": {
       "phpunit/phpunit": "5.4.*"
   },
   "autoload": {
       "classmap": [
           "src/"
       ]
   }
}

Result

$./vendor/bin/phpunit tests/unit/testMyClass.php

PHPUnit 4.8.27 by Sebastian Bergmann and contributors.

.

Time: 252 ms, Memory: 2.25MB

OK (1 test, 0 assertions)

Please let me know if this worked out for you too!

Upvotes: 27

Nikola Bodrozic
Nikola Bodrozic

Reputation: 116

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/

Output:

PHPUnit 6.0.10 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 486 ms, Memory: 14.00MB

OK (2 tests, 3 assertions)

This is working on Symfony 2.8 LTS and PHPunit 6.0 github repo - https://github.com/nikola-bodrozic/PHPUnit-Symfony28

Upvotes: 4

Related Questions