Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in …?

后端 未结 13 1029
攒了一身酷
攒了一身酷 2020-11-29 19:23

Why I\'m getting this PHP error?

Fatal error: Class \'PHPUnit_Framework_TestCase\' not found in ...
相关标签:
13条回答
  • 2020-11-29 19:58

    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 讨论(0)
  • 2020-11-29 19:59

    For me, it was because I ran

    $ phpunit .
    

    instead of

    $ phpunit
    

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

    0 讨论(0)
  • 2020-11-29 20:02

    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).

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

    0 讨论(0)
  • 2020-11-29 20:04

    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.

    0 讨论(0)
  • 2020-11-29 20:06

    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 {
    
    }
    
    0 讨论(0)
  • 2020-11-29 20:06

    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.

    0 讨论(0)
提交回复
热议问题