PHPUnit 错误 - 找不到类


PHPUnit error - Class could not be found

我刚刚在我的系统上安装了 PHPUnit 3.5,从 3.4 升级它,我在使用新版本时遇到了一些问题。当我尝试运行测试时,我总是得到相同的输出。当我尝试在命令行上运行 PHPUnit 手册中的StackTest示例(示例 4.1)时,我得到了以下结果:

> phpunit StackTest
X-Powered-By: PHP/5.2.17
Content-type: text/html
PHPUnit 3.5.13 by Sebastian Bergmann.
Class StackTest could not be found in StackTest.php.

更糟糕的是,当我尝试从 Web 浏览器运行它时,我得到以下输出:

Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /path/to/tests/StackTest.php on line 2 

有谁知道如何设置它?谢谢。

我遇到了你在Windows上描述的问题。问题出在文件pear''PHPUnit''Runner''StandardTestSuiteLoader.php在第131行,这是由条件中文件名中的不同驱动器号大小写引起的

if ($class->getFileName() == realpath($suiteClassFile)) {

我的简单解决方法是将此行更改为不区分大小写

if (strtolower($class->getFileName()) == strtolower(realpath($suiteClassFile))) {
phpunit MyTestClass

就我而言

  • MyTestClass.php 应该在项目主目录中
  • 它应该以长 PHP 开放标签(<?php 开头,而不是<?
  • 它应该包含class MyTestClass extends PHPUnit_Framework_TestCase {

我知道这很可能不是最好的方法,只是指向初学者开始。

尝试

pear upgrade pear

(如果它要求您进行频道升级,请这样做)

然后

pear install --force --alldeps phpunit/phpunit

然后重试。

3.5 升级与有缺陷的梨安装程序相结合(1.9.1 有一个有点烦人的错误,所以请确保您真的在 1.9.2 上)有时会很痛苦。

我认为您的 PHPUnit 类名为 StackTest ,您要测试的类也命名为 StackTest 。这将导致 PHPUnit 中的路径冲突。

使这两个名称不同,您将解决此问题。

就我而言,此问题是由于通过require_once将 PHPUnit 包含在源文件中引起的:

require_once 'phar://phpunit.phar';

删除该行使我的测试用例可以运行。

当您忘记让测试类扩展 PHPUnit TestCase 类时,也可能导致此错误,例如

class MyTestClass extends 'PHPUnit'Framework'TestCase { ...

从 PHPUnit 9 开始,文件名必须与测试中的类名匹配。

#4105:弃用单个文件中的多个测试用例类,并且测试用例类名与文件名不同

因此,具有类名PluginTest test-plugin.php将失败并出现此错误。要修复它,您需要将文件重命名为 PluginTest.php .

错误的错误消息 IMO。

我能够解决问题。这是我加载课程方式的结果。我像这样在参数数组中使用了我的参数,它起作用了。但是类路径等还有很多其他问题,我必须先解决。要查看有效的解决方案,请查看此处(http://www.siteconsortium.com/h/p1.php?id=php002)。

$command = new PHPUnit_TextUI_Command();
$command->run(array('test', 'testCase', 'c:'workspace'project'testCase.php'), true);

听起来 PHPUnit 不在您的包含路径上。 要轻松测试这一点,请尝试以下操作:

$ phpunit --include-path /path/to/PHPUnit StackTest