简体   繁体   English

运行单个测试时找不到PHPUNIT_Framework_TestCase

[英]PHPUNIT_Framework_TestCase not found when running single test

I am trying to use PHPUnit testing with Laravel 5.2.10 . 我正在尝试将PHPUnit测试与Laravel 5.2.10一起使用。 I have PHPUnit 5.1.4 installed locally within my project. 我在项目中本地安装了PHPUnit 5.1.4

When I run: 当我跑步时:

phpunit

PHPUnit runs all tests in my test directory perfectly. PHPUnit可以完美地在我的test目录中运行所有测试。

However, when I run: 但是,当我运行时:

phpunit tests/unit/testThatFails.php

I get: 我得到:

PHP Fatal error:  Class 'PHPUNIT_Framework_TestCase' not found in ...

The strange thing is that when I run phpunit it is succesfully running every test in the test directory, including this testThatFails.php just fine. 奇怪的是,当我运行phpunit时,它成功地运行了测试目录中的每个测试,包括此testThatFails.php就很好了。

Why does it break when I try to run just one test? 当我尝试仅运行一项测试时,为什么会中断?

Update: 更新:
Here is my phpunit.xml : 这是我的phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>
            <directory suffix=".php">app/</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>
</phpunit>

Update: 更新:
For now, since I am using Laravel, I am circumventing this issue by extending TestCase rather than PHPUNIT_Framework_TestCase , and my tests are able to run as expected. 现在,由于我正在使用Laravel,所以通过扩展TestCase而不是PHPUNIT_Framework_TestCase 规避此问题,并且我的测试能够按预期运行。 However, this is just a circumvention and not a solution . 但是,这只是一种规避而不是解决方案 (It is also strange to me that this works given the fact that TestCase itself does ultimately extend from PHPUNIT_Framework_TestCase . (鉴于TestCase本身最终确实从PHPUNIT_Framework_TestCase扩展,这一事实对我来说也很奇怪。

The message says PHPUNIT_Framework_TestCase , but the class is actually called PHPUnit_Framework_TestCase . 该消息显示为PHPUNIT_Framework_TestCase ,但该类实际上称为PHPUnit_Framework_TestCase Now here is the tricky thing 现在这是棘手的事情

  • Class names in PHP are case insensitive, that's why it works when you run the whole test suite and the class is already loaded. PHP中的类名不区分大小写,这就是为什么当您运行整个测试套件并且该类已经加载时它可以工作的原因。 BUT
  • The autoloader is case sensitive, because your file system is case sensitive and it cannot guess the correct case. 自动加载器区分大小写,因为文件系统区分大小写,并且无法猜测正确的大小写。

So when you run the test separately, the class PHPUNIT_Framework_TestCase is not loaded before your test case file is included and the autoloader will look for a file PHPUNIT/Framework/TestCase.php instead of PHPUnit/Framework/TestCase.php . 因此,当您单独运行测试时,在包含测试用例文件之前不会加载类PHPUNIT_Framework_TestCase ,并且自动加载器将查找文件PHPUNIT/Framework/TestCase.php而不是PHPUnit/Framework/TestCase.php This file does not exist and you receive the "class not found" error. 该文件不存在,并且您收到“找不到类”错误。

Solution

Replace 更换

class TestThatFails extends PHPUNIT_Framework_TestCase

with

class TestThatFails extends PHPUnit_Framework_TestCase

PHPUnit loads the configuration file phpunit.xml (see here ) that actually holds the info about the bootstrap file PHPUnit加载配置文件phpunit.xml (请参阅此处 ),该文件实际上包含有关引导文件的信息。

...
bootstrap="tests/bootstrap.php"
...

It is the bootstrap.php that loads the autoloader thus making all of those classes available in your app. bootstrap.php会加载autoloader从而使所有这些类在您的应用程序中可用。

require __DIR__.'/../vendor/autoload.php';

The autoload.php , probably, is not included in the testThatFails.php . autoload.php可能未包含在testThatFails.php The autoload php loads the PHPUNIT_Framework_TestCase along with other files/classes that may be relevant to this case (some other unit test classes, etc.). autoload php会加载PHPUNIT_Framework_TestCase以及与此情况可能相关的其他文件/类(某些其他单元测试类,等等)。 IMHO the best way, other than including it in the phpunit.xml , is to include the bootstrap file as an argument in command 恕我直言,除了将其包含在phpunit.xml ,最好的方法是将引导文件作为命令中的参数包括在内

phpunit --bootstrap bootstrap/autoload.php testThatFails.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM