找不到PHPUnit类TestCase

时间:2016-07-25 00:48:39

标签: php phpunit composer-php

我正在创建一个PHP库,并希望开始编写测试。我收到错误Fatal error: Class 'PHPUnit\Framework\TestCase' not found

我的项目结构是:在我的主目录中,我有composer.json,一个带有我所有类的src /目录,一个带单元/和接受/子目录的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/"
        }
    }
}

4 个答案:

答案 0 :(得分:20)

我遇到了同样的问题,我通过从 PHPUnit_Framework_TestCase 类扩展我的测试类而不是使用命名空间 PHPUnit \ Framework \ TestCase 。重建项目结构后,它对我来说很好。

测试/单元/ testMyClass.php

<?php
require './vendor/autoload.php';

class MyClassTest extends PHPUnit_Framework_TestCase {
     public function testCreateMyClass() {
        // Tests are written here
     }
}
?>

composer.json

{
   "name": "root/project",
   "authors": [
      {
           "name": "watzerm",
           "email": "some.email@provider.at"
      }
   ],
   "require": {
       "phpunit/phpunit": "5.4.*"
   },
   "autoload": {
       "classmap": [
           "src/"
       ]
   }
}

结果

$./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)

请告诉我这是否也适合您!

答案 1 :(得分:3)

我用更新版本的PHPUnit解决了这个问题:

wget https://phar.phpunit.de/phpunit-6.0.phar
php phpunit-6.0.phar -c app/

输出:

PHPUnit 6.0.10 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 486 ms, Memory: 14.00MB

OK (2 tests, 3 assertions)

这适用于Symfony 2.8 LTS和PHPunit 6.0 github repo - https://github.com/nikola-bodrozic/PHPUnit-Symfony28

答案 2 :(得分:0)

我通过以下方式解决了我的问题:

extends PHPUnit\Framework\TestCase

答案 3 :(得分:0)

您需要在项目的根目录运行php bin/phpunit,它将下载所有需要的类,并且PhpStorm错误应该消失了

相关问题