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

后端 未结 13 1030
攒了一身酷
攒了一身酷 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 20:07

    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.

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

    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

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

    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!

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

    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!

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

    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.

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

    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
    
    0 讨论(0)
提交回复
热议问题