簡體   English   中英

未找到 PHPUnit 類 TestCase

[英]PHPUnit class TestCase not found

我正在創建一個 PHP 庫並想開始編寫測試。 我收到一個錯誤Fatal error: Class 'PHPUnit\\Framework\\TestCase' not found

我的項目結構是:在我的主目錄中,我有 composer.json,一個包含我所有類的 src/ 目錄,一個包含 unit/ 和acceptance/ 子目錄的 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/"
        }
    }
}

我遇到了同樣的問題,我通過從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
     }
}
?>

作曲家.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)

如果這對您也有效,請告訴我!

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

我用以下方法解決了我的問題:

extends PHPUnit\Framework\TestCase

確保項目根目錄中存在“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">

就我而言,它解決了問題。

你需要在項目的根目錄下運行php bin/phpunit ,它會下載所有需要的類並且 PhpStorm 錯誤應該消失

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM