php 未找到 PHPUnit 类 TestCase
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38558310/
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
PHPUnit class TestCase not found
提问by Derek Spaulding
I am working on creating a PHP library and want to start writing tests. I am getting an error Fatal error: Class 'PHPUnit\Framework\TestCase' not found
.
我正在创建一个 PHP 库并想开始编写测试。我收到一个错误Fatal error: Class 'PHPUnit\Framework\TestCase' not found
。
My project structure is: in my main directory I have composer.json, a src/ directory with all my classes, a tests/ directory with unit/ and acceptance/ subdirectories. The tests I am trying to run are in the the unit/ directory. I am using the command line interface to run the test so the error happens when running phpunit tests/unit/testMyClass.php
我的项目结构是:在我的主目录中,我有 composer.json,一个包含我所有类的 src/ 目录,一个包含 unit/ 和acceptance/ 子目录的 tests/ 目录。我尝试运行的测试位于 unit/ 目录中。我正在使用命令行界面运行测试,因此运行时发生错误phpunit tests/unit/testMyClass.php
testMyClass.php looks like:
testMyClass.php 看起来像:
<?php
require 'vendor/autoload.php';
use PHPUnit\Framework\TestCase;
class MyClassTest extends TestCase {
public function testCreateMyClass() {
// Tests are written here
}
}
?>
My composer.json is:
我的 composer.json 是:
{
"require-dev": {
"phpunit/phpunit": "4.8.*"
}
"autoload": {
"classmap": [
"src/"
}
}
}
回答by mwatzer
I had the same problem and I solved it by extending my test class from the PHPUnit_Framework_TestCaseclass instead of using the namespacePHPUnit\Framework\TestCase. After rebuilding your project-structure it worked fine for me.
我遇到了同样的问题,我通过从PHPUnit_Framework_TestCase类扩展我的测试类而不是使用命名空间PHPUnit\Framework\TestCase来解决它。重建您的项目结构后,它对我来说效果很好。
tests/unit/testMyClass.php
测试/单元/testMyClass.php
<?php
require './vendor/autoload.php';
class MyClassTest extends PHPUnit_Framework_TestCase {
public function testCreateMyClass() {
// Tests are written here
}
}
?>
composer.json
作曲家.json
{
"name": "root/project",
"authors": [
{
"name": "watzerm",
"email": "[email protected]"
}
],
"require": {
"phpunit/phpunit": "5.4.*"
},
"autoload": {
"classmap": [
"src/"
]
}
}
Result
结果
$./vendor/bin/phpunit tests/unit/testMyClass.php
PHPUnit 4.8.27 by Sebastian Bergmann and contributors.
.
Time: 252 ms, Memory: 2.25MB
OK (1 test, 0 assertions)
Please let me know if this worked out for you too!
如果这对您也有效,请告诉我!
回答by Nikola Bodrozic
I solved the problem with newer version of PHPUnit:
我用较新版本的 PHPUnit 解决了这个问题:
wget https://phar.phpunit.de/phpunit-6.0.phar
php phpunit-6.0.phar -c app/
Output:
输出:
PHPUnit 6.0.10 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)
Time: 486 ms, Memory: 14.00MB
OK (2 tests, 3 assertions)
This is working on Symfony 2.8 LTS and PHPunit 6.0 github repo - https://github.com/nikola-bodrozic/PHPUnit-Symfony28
这适用于 Symfony 2.8 LTS 和 PHPunit 6.0 github repo - https://github.com/nikola-bodrozic/PHPUnit-Symfony28
回答by Marcos Rezende
I solve my problem with:
我用以下方法解决了我的问题:
extends PHPUnit\Framework\TestCase