使用 2 个或更多方法时,Laravel 中的 PHPUnit 测试失败

标签 php laravel unit-testing

所以我在 Laravel 中的单元测试遇到了一个非常特殊的问题。我想做的是一些基本的单元测试,所以我用我的测试设置了一个类。当我有一个运行测试的方法时,测试工作得很好。类看起来像这样:

class Pixel_BasicTest extends Illuminate\Foundation\Testing\TestCase {


public function createApplication()
{
    $app = require __DIR__.'/../bootstrap/app.php';
    $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
    return $app;
}

public function testRoutes(){
    echo "Start of testRoutes\n";

    //Test all the GET routes we have
    $routeCollection = Route::getRoutes();
    $num_404s = 0;
    $path_404s = [];
    ...various other code that does some tests, these all work right
}
}

现在,当我尝试添加另一个功能时,就会出现问题。我的类(class)现在看起来像这样:

class Pixel_BasicTest extends Illuminate\Foundation\Testing\TestCase {


public function createApplication()
{
    $app = require __DIR__.'/../bootstrap/app.php';
    $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
    return $app;
}

public function testRoutes(){
    echo "Start of testRoutes\n";

    //Test all the GET routes we have
    $routeCollection = Route::getRoutes();
    $num_404s = 0;
    $path_404s = [];
    ...various other code that does some tests, these all work right
}

public function testSecondThing() {
    echo "Start of testSecondThing";
    ...some code testing something else
}


}

现在,我知道第二个函数中的代码可以工作,因为当我将该代码放入第一个函数中并完全删除第二个函数时,测试可以完美运行。但是,当我将代码放入第二个函数时,它失败了,我的终端窗口如下所示:

Mikes-MacBook-Air-96:hq pixelmember$ ./vendor/bin/phpunit -v --debug
PHPUnit 4.8.29 by Sebastian Bergmann and contributors.

Runtime:    PHP 5.6.10
Configuration:  /Users/pixelmember/Documents/hq/phpunit.xml


Starting test 'Pixel_BasicTest::testRoutes'.
.Start of testRoutes
End of testRoutes

Starting test 'Pixel_BasicTest::testAfterschool'.
Mikes-MacBook-Air-96:hq pixelmember$ 

如您所见,它到达第二个函数,然后停止或失败或发生其他情况。没有错误消息,而且我也没有看到我在函数开头的代码中编写的消息(回显“Starting testSecondThing()”部分)

老实说我不知道​​该怎么办。我现在可以不用单元测试,并且可以处理应用程序的其他部分,但我真的希望能够使用单元测试,而不必将每个测试都放在单个函数中。

如果有帮助,这是我的 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">
        <file>./tests/Pixel_BasicTest.php</file>
    </testsuite>
</testsuites>
<filter>
    <blacklist>
        <directory>./vendor</directory>
    </blacklist>
</filter>
<php>
    <env name="APP_ENV" value="testing"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="SESSION_DRIVER" value="array"/>
    <env name="DB_DATABASE" value="test"/>
    <env name="DB_USERNAME" value="root"/>
    <env name="DB_PASSWORD" value=""/>
</php>

除了添加一些与数据库相关的环境变量之外,我并没有真正编辑或更改此文件的大部分内容。

编辑:我解决了问题,请参阅下面的修复

最佳答案

哇,我实际上刚刚想通了。有时候只是需要休息一下,哈哈。

如果有人需要帮助解决类似问题,我为解决问题所做的就是更改我的 phpunit.xml 文件。我将设置“processIsolation”更改为 true,因此我的 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="true"
      stopOnFailure="false"
      syntaxCheck="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <file>./tests/Pixel_BasicTest.php</file>
        </testsuite>
    </testsuites>
    <filter>
        <blacklist>
            <directory>./vendor</directory>
        </blacklist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="DB_DATABASE" value="test"/>
        <env name="DB_USERNAME" value="root"/>
        <env name="DB_PASSWORD" value=""/>
    </php>
</phpunit>

我仍然好奇为什么这会起作用,以及这个设置到底有什么作用?我知道这会在单独的 PHP 进程中运行每个测试,但我不确定为什么这会帮助/解决问题,以及这是否真正解决了我的问题,或者以后是否会遇到更多问题。

如果对 PHPUnit 有更多经验的人能够对此有所了解,我们将不胜感激。

编辑:我使用的文档的一些链接: phpunit.xml 配置文件: https://phpunit.de/manual/current/en/appendixes.configuration.html 一些设置的定义: https://phpunit.de/manual/current/en/textui.html#textui.clioptions

关于使用 2 个或更多方法时,Laravel 中的 PHPUnit 测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41248664/

相关文章:

php - YouTube网址的preg_match正则表达式部分失败

mysql - Laravel Eloquent 和 3 表关系

Python 单元测试在失败后继续

javascript - 如何用 'v-for' 实现 'v-if' ?

javascript - ajax响应创建laravel删除按钮

javascript - 在 Javascript 测试中断言对象相等 (chai)

unit-testing - 使用 axios-mock-adapter 模拟 axios 得到未定义的响应

php - 悬停时 CSS 边框问题

php - NetBeans 调试不适用于 phpunit 测试

php - stream_socket_client 在我的服务器上失败,无法了解有关错误的更多信息