PHPUnit 找不到 "TestCase" class

PHPUnit can't found the "TestCase" class

为了 运行 我使用项目的 PHPUnit 进行的测试,我执行以下操作:php vendor/bin/phpunit tests/SomeClassTest.php 鉴于以下 class 声明,它工作正常:

class SomeClassTest extends PHPUnit_Framework_TestCase {
  public function test_someMethod() {}
}

但是我这样做失败了:

use PHPUnit\Framework\TestCase;

class SomeClassTest extends TestCase {
  public function test_someMethod() {}
}

我得到 PHP Fatal error: Class 'PHPUnit\Framework\TestCase' not found...

Class TestCase 从 PHPUnit 5.4 开始存在。如果您在 2. Writing Tests for PHPUnit 部分设置 5.3 tag (look for ForwardCompatibility folder) or you can compare doc for 5.3 and 5.4,您可以在 github 上看到它:

"ClassTest inherits (most of the time) from PHPUnit_Framework_TestCase." 用于 PHPUnit 5.3

"ClassTest inherits (most of the time) from PHPUnit\Framework\TestCase." 用于 PHPUnit 5.4

在我仍然标记为可由 PHP 5.4 使用的库中,我必须将其添加到我的 top-level 测试用例 class 以桥接 non-namespaced / 命名空间差异,取决于 Composer 根据运行时 PHP 版本安装的 PHPUnit 版本。

/*
 * Allow for PHPUnit 4.* while XML_Util is still usable on PHP 5.4
 */
if (!class_exists('PHPUnit_Framework_TestCase')) {
    class PHPUnit_Framework_TestCase extends \PHPUnit\Framework\TestCase {}
}

abstract class AbstractUnitTests extends \PHPUnit_Framework_TestCase
{

这适用于 PHP 5.4(PHPUnit 4.8.34)到 PHP 7.1(PHPUnit 6.0.2)。