143

Why I'm getting this PHP error?

Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...
Alvin
  • 1,850
  • 4
  • 15
  • 17
  • 10
    You are not providing any information at all. – Pekka May 19 '11 at 22:55
  • Run your tests via the phpunit command-line. It loads up the necessary classes. – Halfstop May 24 '16 at 17:05
  • You can also use the answer from the following answer https://stackoverflow.com/questions/42811164/class-phpunit-framework-testcase-not-found/42828632#42828632 to keep your code compatible with PHPUnit 6 as well as earlier versions. – Robson Jun 15 '17 at 16:25

14 Answers14

281

For those arriving here after updating phpunit to version 6 or greater released on 2017-02-03 (e.g. with composer), you may be getting this error because phpunit code is now namespaced (check changelog changelog ).

You will need to refactor things like \PHPUnit_Framework_TestCase to \PHPUnit\Framework\TestCase

Update 2023-03-30: updated changelog link based on comment

Shadi
  • 9,742
  • 4
  • 43
  • 65
  • 4
    Thanks, I had this problem due to Travis CI using the most recent PHPUnit version on their PHP 7 builds. My fix was to manually download an old phar and use that instead. – DisgruntledGoat Mar 13 '17 at 17:12
  • 2
    https://github.com/sebastianbergmann/phpunit/wiki/Release-Announcement-for-PHPUnit-6.0.0 it's on the top of the release notes, yet I missed it – Qchmqs Mar 15 '17 at 09:17
  • Thank ye kindly, good sir. I am indeed arriving here after upgrading to version 6. – h4ckNinja May 13 '17 at 04:20
  • I was getting really annoyed that my tests suddenly stopped working. I did a composer update and it updated my PHPunit to 6.1. Thank You. – Jed Lynch Sep 01 '17 at 18:26
  • Here is an update link as linked file is gone in phpunit repository: (check [changelog](https://github.com/sebastianbergmann/phpunit/blob/9d0c024d2099531442d862b66b0ad7cf35ed8e78/ChangeLog-6.0.md)) – Axi Mar 29 '23 at 13:39
74

The PHPUnit documentation says used to say to include/require PHPUnit/Framework.php, as follows:

require_once ('PHPUnit/Framework/TestCase.php');

UPDATE

As of PHPUnit 3.5, there is a built-in autoloader class that will handle this for you:

require_once 'PHPUnit/Autoload.php';

Thanks to Phoenix for pointing this out!

Thomas Vander Stichele
  • 36,043
  • 14
  • 56
  • 60
defines
  • 10,229
  • 4
  • 40
  • 56
  • 14
    PHPUnit 3.5 comes with its own autoloader that might help keep things simple: `require_once 'PHPUnit/Autoload.php'` –  May 20 '11 at 00:30
  • +1 Phoenix, I also recommend reading up on autoloading in PHP it can be fairly useful to write one's own autoloader script. – defines May 20 '11 at 11:49
  • 1
    I concur that require_once('PHPUnit/Autoload.php') is the best practice. – Paul Maidment Dec 05 '11 at 12:24
  • 1
    I don't have both `PHPUnit/Autoload.php` and `PHPUnit/Framework/TestCase.php`, my folder is kind of like `PHPUnit/Framework/MockObject` – Sufendy Dec 15 '11 at 04:35
  • Looks like the documentation has changed since I posted this answer. Sorry I can't really offer more insight, I haven't recently used PHPUnit - I just Googled the answer for this one ;) – defines Dec 15 '11 at 22:11
  • From where it will load the PHPUnit/Autoload.php and PHPUnit/Framework/TestCase.php as my folder only contains GShoppingContent.php as I want to use google base to upload items automatically – Musaddiq Khan Jan 07 '14 at 05:27
  • 5
    and now I have `PHP Fatal error: require_once(): Failed opening required 'PHPUnit/Autoload.php'` – Dennis Mar 19 '14 at 14:26
  • 2
    see @shadi's answer for PHP 6+ http://stackoverflow.com/a/42561590/2883328 – Dennis Apr 05 '17 at 18:56
52

For higher version of phpunit such as 6.4 You must use the namespace PHPUnit\Framework\TestCase

use TestCase instead PHPUnit_Framework_TestCase

// use the following namespace
use PHPUnit\Framework\TestCase;

// extend using TestCase instead PHPUnit_Framework_TestCase
class SampleTest extends TestCase {

}
Jijesh Cherayi
  • 1,111
  • 12
  • 15
16

I was running PHPUnit tests on PHP5, and then, I needed to support PHP7 as well. This is what I did:

In composer.json:

"phpunit/phpunit": "~4.8|~5.7"

In my PHPUnit bootstrap file (in my case, /tests/bootstrap.php):

// PHPUnit 6 introduced a breaking change that
// removed PHPUnit_Framework_TestCase as a base class,
// and replaced it with \PHPUnit\Framework\TestCase
if (!class_exists('\PHPUnit_Framework_TestCase') && class_exists('\PHPUnit\Framework\TestCase'))
    class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');

In other words, this will work for tests written originally for PHPUnit 4 or 5, but then needed to work on PHPUnit 6 as well.

The Onin
  • 5,068
  • 2
  • 38
  • 55
13

You may get this error because you namespaced the file. If so you will need to specify that PHPUnit_Framework_TestCase is in the global namespace by preceding it with a backslash:

namespace AcmeInc\MyApplication\Tests
class StackTest extends \PHPUnit_Framework_TestCase {}

I submitted a crude PR to start conversation for correcting the documentation.

Bevan
  • 483
  • 4
  • 5
  • 1
    Yes, if you did this: `class YourNiceTest extends PHPUnit_Framework_TestCase` just add the \ in front of the extended class, like in `class YourNiceTest extends \PHPUnit_Framework_TestCase` - This worked for me, using `Symfony 2.8` and including the `phpunit` in the composer file downloaded as a local dependency with `"phpunit/phpunit": "^4.8"` – Xavi Montero Dec 31 '15 at 13:02
6

You can simply install PHPUnit to run commands (https://github.com/sebastianbergmann/phpunit/#php-archive-phar):

wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit

Run single test

And then run PHPunit test:

phpunit test.php

Content of test file is following:

<?php

class StackTest extends PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
    }

    public function testSave()
    {

    }
}

Run test suite

Configuration of test suite: demosuite.xml. demo is directory containing all tests. Test files must be named as *_test.php (suffix).

<testsuites>
    <testsuite name="DemoTestSuite">
        <directory suffix="test.php">demo</directory>
    </testsuite>
</testsuites>

Test suite runs with following commands:

phpunit -c demosuite.xml --testsuite DemoTestSuite
radeklos
  • 2,168
  • 21
  • 19
  • 4
    This no longer works because latests PHPUnit no longer supports old class names such as `PHPUnit_Framework_TestCase`. You really have to use `... extends PHPUnit\Framework\TestCase` – Mikko Rantalainen Jun 01 '18 at 06:16
  • Just saying that I had to first `mv phpunit /usr/local/bin/phpunit` and then `sudo chmod +x /usr/local/bin/phpunit`. Setting file as executable before moving it never worked on Vagrant Ubuntu. No idea why... But in case someone is struggling with this, I hope this will help. – George Mylonas Dec 12 '18 at 09:00
4

Assumption:

Phpunit (3.7) is available in the console environment.

Action:

Enter the following command in the console:

SHELL> phpunit "{{PATH TO THE FILE}}"

Comments:

You do not need to include anything in the new versions of PHPUnit unless you do not want to run in the console. For example, running tests in the browser.

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
Salman Virk
  • 12,007
  • 9
  • 36
  • 47
1

I use ZF2 and work for me when replaced 'PHPUnit_Framework_TestCase' to '\PHPUnit\Framework\TestCase'

1

I got it working with

include("vendor/autoload.php");

at the top of my test function.

Spinstaz
  • 287
  • 6
  • 12
0

If you have Centos or other Linux distribution you have to install phpunit package, I did that with yum install phpunit and it worked. Maybe you can have to add a repository, but I think it has to work smooth with the default ones (I have CentOS 7)

0

It may well be that you're running WordPress core tests, and have recently upgraded your PhpUnit to version 6. If that's the case, then the recent change to namespacing in PhpUnit will have broken your code.

Fortunately, there's a patch to the core tests at https://core.trac.wordpress.org/changeset/40547 which will work around the problem. It also includes changes to travis.yml, which you may not have in your setup; if that's the case then you'll need to edit the .diff file to ignore the Travis patch.

  1. Download the "Unified Diff" patch from the bottom of https://core.trac.wordpress.org/changeset/40547
  2. Edit the patch file to remove the Travis part of the patch if you don't need that. Delete from the top of the file to just above this line:

    Index: /branches/4.7/tests/phpunit/includes/bootstrap.php
    
  3. Save the diff in the directory above your /includes/ directory - in my case this was the Wordpress directory itself

  4. Use the Unix patch tool to patch the files. You'll also need to strip the first few slashes to move from an absolute to a relative directory structure. As you can see from point 3 above, there are five slashes before the include directory, which a -p5 flag will get rid of for you.

    $ cd [WORDPRESS DIRECTORY]
    $ patch -p5 < changeset_40547.diff 
    

After I did this my tests ran correctly again.

piersb
  • 609
  • 9
  • 22
0

NOTICE: Command php bin/console generate:doctrine:crud also create TestController in src/Tests so it can throw error when you tried to start server if you don't have UnitTests. Remove the file fix it!

MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
0

For me, it was because I ran

$ phpunit .

instead of

$ phpunit

when I already had a configured phpunit.xml file in the working directory.

cyberbit
  • 1,345
  • 15
  • 22
0

I am using php 5.6 on window 10 with zend 1.12 version for me adding

require_once 'PHPUnit/Autoload.php';

before

abstract class Zend_Test_PHPUnit_ControllerTestCase extends PHPUnit_Framework_TestCase

worked. We need to add this above statement in ControllerTestCase.php file

Shankar
  • 29
  • 1
  • 4