laravel 运行单个测试时找不到 PHPUNIT_Framework_TestCase

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35002658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:06:19  来源:igfitidea点击:

PHPUNIT_Framework_TestCase not found when running single test

phplaravelphpunit

提问by tam5

I am trying to use PHPUnit testing with Laravel 5.2.10. I have PHPUnit 5.1.4installed locally within my project.

我正在尝试将 PHPUnit 测试与Laravel 5.2.10一起使用。我在我的项目中本地安装了PHPUnit 5.1.4

When I run:

当我运行时:

phpunit

PHPUnit runs all tests in my testdirectory perfectly.

PHPUnittest完美地运行了我目录中的所有测试。

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.phpjust fine.

奇怪的是,当我运行 phpunit 时,它成功地运行了 test 目录中的每个测试,包括这个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 circumventingthis issue by extending TestCaserather than PHPUNIT_Framework_TestCase, and my tests are able to run as expected. However, this is just a circumventionand not a solution. (It is also strange to me that this works given the fact that TestCaseitself does ultimately extend from PHPUNIT_Framework_TestCase.

更新:
目前,由于我使用的是 Laravel,我通过扩展而不是 来规避这个问题,并且我的测试能够按预期运行。然而,这只是一种规避而不是解决方案。(鉴于它本身最终确实从.TestCasePHPUNIT_Framework_TestCaseTestCasePHPUNIT_Framework_TestCase

回答by Fabian Schmengler

The message says PHPUNIT_Framework_TestCase, but the class is actually called PHPUnit_Framework_TestCase. Now here is the tricky thing

消息说PHPUNIT_Framework_TestCase,但该类实际上是被调用的PHPUnit_Framework_TestCase。现在这是棘手的事情

  • 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. BUT
  • The autoloader is case sensitive, because your file system is case sensitive and it cannot guess the correct case.
  • PHP 中的类名不区分大小写,这就是为什么当您运行整个测试套件并且类已经加载时它可以工作的原因。
  • 自动加载器区分大小写,因为您的文件系统区分大小写,它无法猜测正确的大小写。

So when you run the test separately, the class PHPUNIT_Framework_TestCaseis not loaded before your test case file is included and the autoloader will look for a file PHPUNIT/Framework/TestCase.phpinstead of PHPUnit/Framework/TestCase.php. This file does not exist and you receive the "class not found" error.

因此,当您单独运行测试时,在PHPUNIT_Framework_TestCase包含您的测试用例文件之前不会加载该类,并且自动加载器将查找文件PHPUNIT/Framework/TestCase.php而不是PHPUnit/Framework/TestCase.php. 此文件不存在,您会收到“找不到类”错误。

Solution

解决方案

Replace

代替

class TestThatFails extends PHPUNIT_Framework_TestCase

with

class TestThatFails extends PHPUnit_Framework_TestCase

回答by sitilge

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.phpthat loads the autoloaderthus 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. The autoload phploads the PHPUNIT_Framework_TestCasealong with other files/classes that may be relevant to this case (some other unit test classes, etc.). IMHO the best way, other than including it in the phpunit.xml, is to include the bootstrap file as an argument in command

autoload.php,大概,是不包括在testThatFails.php。的autoload php负载PHPUNIT_Framework_TestCase与其他文件/类可能相关于该情况(其他一些单元测试类等)沿。恕我直言,除了将其包含在 中之外phpunit.xml,最好的方法是将引导程序文件作为参数包含在命令中

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