【发布时间】:2016-11-28 05:43:09
【问题描述】:
我正在创建一个 PHP 库并想开始编写测试。我收到一个错误Fatal error: Class 'PHPUnit\Framework\TestCase' not found。
我的项目结构是:在我的主目录中,我有 composer.json,一个包含我所有类的 src/ 目录,一个包含 unit/ 和 acceptor/ 子目录的 tests/ 目录。我尝试运行的测试位于 unit/ 目录中。我正在使用命令行界面运行测试,所以在运行phpunit tests/unit/testMyClass.php时会出现错误
testMyClass.php 看起来像:
<?php
require 'vendor/autoload.php';
use PHPUnit\Framework\TestCase;
class MyClassTest extends TestCase {
public function testCreateMyClass() {
// Tests are written here
}
}
?>
我的 composer.json 是:
{
"require-dev": {
"phpunit/phpunit": "4.8.*"
}
"autoload": {
"classmap": [
"src/"
}
}
}
【问题讨论】:
-
你运行
composer install了吗?您的 testMyClass.php 文件是否位于供应商目录的同一目录中(您还运行composer install)? -
有问题的类不遵循您试图从中调用它的命名空间。如果愿意,请尝试
use PHPUnit_Framework_TestCase as TestCase;,否则只需扩展完整的类名。
标签: php phpunit composer-php