Laravel 4.2:测试用例自动加载

Ala*_*orm 5 php phpunit laravel

现在,当我为Laravel应用程序设置一个新测试时,它从基TestCase类扩展而来

class SomeTest extends TestCase
{
}
Run Code Online (Sandbox Code Playgroud)

我想创建一个名为的新基础测试类AnotherTestCase,因此我可以创建共享setup/teardown/helper方法/等的测试用例...

class SomeTest extends AnotherTestCase
{
}
Run Code Online (Sandbox Code Playgroud)

但是,当我跑

phpunit app/tests/SomeTest.php
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

PHP Fatal error:  Class 'AnotherTestCase' not found in /[...]/app/tests/SomeTest.php on line 3
Run Code Online (Sandbox Code Playgroud)

尽管事实上我已定义了一个类

#File: app/tests/AnotherTestCase.php
<?php
class AnotherTestCase extends TestCase
{
}
Run Code Online (Sandbox Code Playgroud)

这很令人困惑,因为phpunit似乎自动加载了这个TestCase类.

我是否需要在自定义基础测试类中手动要求,或者有没有办法告诉phpunit我的新基础测试类?换句话说,为什么phpunit会自动加载TestCase,但不会自动加载AnotherTestCase

Jef*_*ert 7

您可以通过将此添加到您的composer.json:来解决此错误:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/filters",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/tests/AnotherTestCase.php"  // <-- Add Me
    ],
// ...
Run Code Online (Sandbox Code Playgroud)

之后一定要确实做到了composer dump-autoload.我刚刚通过添加以下类来测试它:

class AnotherTestCase extends TestCase {}
Run Code Online (Sandbox Code Playgroud)

并改变了我现有的一个测试,将其作为其父级使用.我认为进入composer.json是你能够加载的方式TestCase.