【问题标题】:PHPUnit class TestCase not found未找到 PHPUnit 类 TestCase
【发布时间】: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


【解决方案1】:

我遇到了同样的问题,我通过从 PHPUnit_Framework_TestCase 类扩展我的测试类而不是使用 namespace 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)

如果这对你也有用,请告诉我!

【讨论】:

    【解决方案2】:

    我用较新版本的 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

    【讨论】:

    • 但是这个版本只能在 PHP7+ 上运行。该操作没有提及他使用的版本。
    【解决方案3】:

    我解决了我的问题:

    extends PHPUnit\Framework\TestCase
    

    【讨论】:

    • "php bin/phpunit" 将为您提供“Sebastian Bergmann 和贡献者的 PHPUnit 9.5.1 ......”
    【解决方案4】:

    确保“phpunit.xml”文件存在于项目根目录中。在这个文件中必须有一个带有“bootstrap =”vendor/autoload.php”属性的“phpunit”标签。
    类似的东西:

    <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true">
    

    就我而言,它解决了问题。

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 2017-02-24
        • 1970-01-01
        • 2016-09-09
        • 1970-01-01
        • 1970-01-01
        • 2013-07-18
        • 2016-10-17
        • 2013-04-11
        • 2016-01-09
        相关资源
        最近更新 更多