未找到 TestCase 的 Laravel 致命错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21726963/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:01:07  来源:igfitidea点击:

laravel fatal error for TestCase not found

phplaravel

提问by Steve Bals

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

if i run laravel it shows the following error

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

Fatal error: Class 'TestCase' not found in D:\xampp\htdocs\laravel\app\tests\ExampleTest.php on line 3 , any idea

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

回答by Farid Movsumov

Run following command in your project's root directory

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

phpunit app/tests/

Update:

更新:

You can also run with the following command if phpunit is not installed on your machine. Which is also better to avoid version differences between team mates.

如果您的机器上没有安装 phpunit,您也可以使用以下命令运行。这也更好地避免了队友之间的版本差异。

vendor/bin/phpunit

enter image description here

在此处输入图片说明

回答by álvaro Guimar?es

Check if your testsdirectory is listed on the classmapproperty of your composer.jsonfile.

检查您的tests目录是否列在文件的classmap属性中composer.json

If not add it and run composer dump-autoload.

如果没有添加它并运行composer dump-autoload

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

回答by Wallace Maxters

In my case, i noticed that my root folder not contained the phpunit.xml file.

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

Solve it including the following code in phpunit.xml at the root folder:

解决它,包括根文件夹下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>

回答by alexrussell

You haven't really given much information to go on here, but the specific problem is probably that you don't have an autoloader (thus the TestCaseclass that PHP is expecting isn't actually loaded. The underlying problem in turn is likely that you're trying to run ExampleTest.phpdirectly on the terminal, whereas instead you must bootstrap a test environment correctly through Laravel and PHPUnit.

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

You should find a phpunit.xml file in the root of your Laravel install. This file tells PHPUnit how to bootstrap and start executing tests for your application, so you should just be able to run phpuniton the command line in the root of your application and it should work.

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

If that's not the issue, please do provide more detail for people to help with.

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

回答by Angel M.

I had same issue, you should run test from root folder, in your case 'http://localhost/laravel' and in terminal you just need to write phpunit, and test will be executed.

我遇到了同样的问题,您应该从根文件夹运行测试,在您的情况下为“ http://localhost/laravel”,在终端中您只需要编写 phpunit,然后将执行测试。

回答by rust

Had the same issue with PHPUnit, he would output always the same message : PHP Fatal error: Class 'Tests\TestCase' not found ...and indeed I think he had troubles to find that class so what you need to do is transform one line :

PHPUnit 有同样的问题,他总是输出相同的消息:PHP Fatal error: Class 'Tests\TestCase' not found ...事实上,我认为他很难找到那个类,所以你需要做的是转换一行:

From this :

由此 :

<?php

namespace Tests\Feature;

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

class AnotherTest extends TestCase {
    public function testExample() {
        // Do something
    }
}

To this :

对此:

<?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() {
        // Do something
    }
}

Notice the first line in use and you should do this if there's any other files in the same case

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

回答by apelidoko

I got the same error but none worked,

我遇到了同样的错误,但没有成功,

except for this one, this worked for me,

除了这个,这对我有用,

I tried reinstalling and upgrading my phpunit in Laravel 5.4,

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

  1. modify compose.json -> find these line and change the phpunit version

    "require-dev": { "phpunit/phpunit": "~6.4" },

  2. after editing, run on your cmd,

    composer update

  3. then run

    composer dump-autoload

  4. php artisan config:clear

  5. php artisan cache:clear

  6. finally, run phpunit

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

    "require-dev": { "phpunit/phpunit": "~6.4" },

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

    作曲家更新

  3. 然后运行

    作曲家转储自动加载

  4. php artisan config:clear

  5. php artisan cache:clear

  6. 最后,运行 phpunit

or run specific unit, phpunit --filter yourTestCaseClass

或运行特定单元, phpunit --filter yourTestCaseClass

enter image description here

在此处输入图片说明

回答by Omebe Johnbosco

The is caused by an outdated phpunit. If you using a globally installed phpunit, here is a link on how to update it https://stackoverflow.com/a/40701333/3792206. Or you can explicitly use the phpunit that was installed with the project.

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