【问题标题】:Setting process isolation to false in phpunit.xml causes Laravel Functional Tests to fail在 phpunit.xml 中将进程隔离设置为 false 会导致 Laravel 功能测试失败
【发布时间】:2017-08-31 18:05:18
【问题描述】:

在我们之前的应用程序中,使用 Laravel 5.3,将 processIsolation 设置为 false 可以加快我们的测试速度而不会导致任何错误:

phpunit.xml:
<phpunit backupGlobals="false"
     ...
     processIsolation="false"
     stopOnFailure="false">

在我们当前的项目中,在 laravel 5.4 中,将 processIsolation 设置为 false 会导致我们所有的功能测试失败(尽管它们确实更快)(为简洁起见删除了一些):

5) Tests\Feature\CallsToActionApiControllerTest::testUsers
Symfony\Component\HttpKernel\Exception\NotFoundHttpException:
/johnny/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:161
/johnny/vendor/laravel/framework/src/Illuminate/Routing/Router.php:533
/johnny/vendor/laravel/framework/src/Illuminate/Routing/Router.php:512
/johnny/vendor/laravel/framework/src/Illuminate/Routing/Router.php:498
/johnny/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:174
/johnny/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
/johnny/vendor/barryvdh/laravel-cors/src/HandleCors.php:34
/johnny/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148
...
/johnny/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:102
/johnny/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:149
/johnny/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:116
/johnny/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:234
/johnny/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:206
/johnny/tests/Functional/CallsToAction/CallsToActionApiControllerTest.php:142

...
7) Tests\Feature\EmailEndpointControllerTest::testCaptureEventsWithBlankRequest
Expected status code 200 but received 404.
Failed asserting that false is true.

/johnny/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:55
/johnny/tests/Functional/Email/EmailActionApiControllerTest.php:88

8) Tests\Feature\EmailEndpointControllerTest::testUnsubscribe
Expected status code 200 but received 404.
Failed asserting that false is true.

/johnny/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:55
/johnny/tests/Functional/Email/EmailActionApiControllerTest.php:96

9) Tests\Feature\EmailEndpointControllerTest::testUnsubscribeWithAlreadyUnsubscribedEmail
Expected status code 200 but received 404.
Failed asserting that false is true.

/johnny/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:55
/johnny/tests/Functional/Email/EmailActionApiControllerTest.php:108

ERRORS!
Tests: 72, Assertions: 79, Errors: 21, Failures: 9.
Script ./vendor/bin/phpunit handling the test event returned with error code 2

都是404错误,或者NotFoundHttpException错误。

我们早期的工作项目是 laravel 5.3 和 phpunit 5.5;我们当前损坏的项目是 laravel 5.4 和 phpunit 5.7。我注意到两个 laravel 之间的一个变化是 app.php 中的应用程序 URL 更改为

'url' => 'http://localhost'

'url' => env('APP_URL', 'http://localhost'),

但改回来并没有做任何事情。请帮忙——我们的测试需要 10 分钟而不是 10 秒。

【问题讨论】:

    标签: php phpunit laravel-5.4


    【解决方案1】:

    我知道这是一篇古老的帖子,但我遇到了同样的问题,我先找到了这个页面,然后才找到解决方案,所以如果有人遇到同样的问题并先停在这里,我希望这个解决方案对你有用。

    感谢 boydcl,该解决方案对我有用 https://github.com/dingo/api/issues/1243

    就我而言,我使用的是“require_once”而不是“require”,这解决了我的问题,

    $path = __DIR__ . '/../routes/api.php';
    
    if (defined('PHPUNIT_TESTING_SESSION')) {
        require $path;
    
        return;
    }
    
    require_once $path;
    

    您可以像这样在 phpunit.xml 中添加常量“PHPUNIT_TESTING_SESSION”:

    <const name="PHPUNIT_TESTING_SESSION" value="true"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多