找不到TestCase的laravel致命错误

时间:2014-02-12 11:41:40

标签: php laravel

http://localhost/laravel/app/tests/ExampleTest.php

如果我运行laravel它会显示以下错误

致命错误:在第3行的D:\ xampp \ htdocs \ laravel \ app \ tests \ ExampleTest.php中找不到类'TestCase',任何想法

9 个答案:

答案 0 :(得分:17)

在项目的根目录中运行以下命令

phpunit app/tests/

<强>更新

如果您的计算机上未安装phpunit,您也可以使用以下命令运行。哪个也更好,以避免队友之间的版本差异。

vendor/bin/phpunit

enter image description here

答案 1 :(得分:6)

就我而言,我注意到我的根文件夹中没有包含phpunit.xml文件。

在根文件夹的phpunit.xml中解决它,包括以下代码:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false"
>
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./app/tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>

答案 2 :(得分:5)

您还没有真正提供太多信息,但具体问题可能是您没有自动加载器(因此PHP所期望的TestCase类实际上并未加载。反过来的问题可能是你试图直接在终端上运行ExampleTest.php,而你必须通过Laravel和PHPUnit正确地引导测试环境。

您应该在Laravel安装的根目录中找到phpunit.xml文件。这个文件告诉PHPUnit如何引导程序并开始为你的应用程序执行测试,所以你应该只能在应用程序根目录的命令行上运行phpunit,它应该可以工作。

如果这不是问题,请为人们提供更多详细信息。

答案 3 :(得分:3)

我有同样的问题,你应该从根文件夹运行测试,在你的情况下&#39; http://localhost/laravel&#39;在终端你只需要写phpunit,测试就会执行。

答案 4 :(得分:3)

检查tests文件的classmap属性中是否列出了composer.json目录。

如果不添加并运行composer dump-autoload

"autoload-dev": {
    "classmap": [
        "tests",
        "database/"
    ]
},

答案 5 :(得分:1)

我得到了同样的错误,但都没有用,

除了这个,这对我有用,

我尝试在Laravel 5.4中重新安装和升级我的phpunit,

  1. 修改compose.json - &gt;找到这些行并更改phpunit版本

    &#34; require-dev&#34;:{         &#34; phpunit / phpunit&#34;:&#34; ~6.4&#34;     },

  2. 编辑后
  3. ,在你的cmd上运行,

    作曲家更新

  4. 然后运行

    composer dump-autoload

  5. php artisan config:clear

  6. php artisan cache:clear

  7. 最后, 运行phpunit

  8. 或运行特定单位phpunit --filter yourTestCaseClass

    enter image description here

答案 6 :(得分:1)

是由过时的phpunit引起的。如果您使用全局安装的phpunit,这里有一个关于如何更新它的链接https://stackoverflow.com/a/40701333/3792206。或者您可以明确使用随项目安装的phpunit。

答案 7 :(得分:0)

PHPUnit遇到相同的问题,他将始终输出相同的消息:PHP Fatal error: Class 'Tests\TestCase' not found ...,实际上我认为他很难找到该类,因此您需要做的是转换一行:

从此(旧版本):

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class AnotherTest extends TestCase {
    public function testExample() {
    }
}

此(新版本):

<?php

namespace Tests\Feature;

use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class AnotherTest extends TestCase {
    public function testExample() {
    }
}
  

请注意第一行正在使用,如果在同一情况下还有其他文件,则应该这样做

答案 8 :(得分:0)

我的 laravel phpunit 正在工作,然后突然开始给我这个错误。再次运行 composer install 解决了。

相关问题