21

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/"
        }
    }
}
2
  • Did you run composer install? Does your testMyClass.php file is in the same directory of your vendor directory (where you also run the composer install)? Commented Jul 25, 2016 at 2:18
  • The class in question doesn't follow the namespaces that you're trying to call it from. Try use PHPUnit_Framework_TestCase as TestCase; if you want to, otherwise just extend the full class name. Commented Jul 25, 2016 at 2:49

5 Answers 5

28

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!

Sign up to request clarification or add additional context in comments.

Comments

4

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

1 Comment

But this version only runs on PHP7+. The op didn't mention what version he uses though.
1

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.

Comments

0

I solve my problem with:

extends PHPUnit\Framework\TestCase

1 Comment

"php bin/phpunit" will give you info as "PHPUnit 9.5.1 by Sebastian Bergmann and contributors.... "
-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.