在 phpunit.xml 中将进程隔离设置为 false 会导致 Laravel 功能测试失败

whi*_*ok6 5 php phpunit laravel-5.4

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

phpunit.xml:
<phpunit backupGlobals="false"
     ...
     processIsolation="false"
     stopOnFailure="false">
Run Code Online (Sandbox Code Playgroud)

在我们当前的项目中,在 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
Run Code Online (Sandbox Code Playgroud)

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

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

'url' => 'http://localhost'
Run Code Online (Sandbox Code Playgroud)

'url' => env('APP_URL', 'http://localhost'),
Run Code Online (Sandbox Code Playgroud)

但改回来并没有什么作用。请帮忙——我们的测试需要 10 分钟而不是 10 秒。

小智 2

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

该解决方案对我有用,感谢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;
Run Code Online (Sandbox Code Playgroud)

您可以在 phpunit.xml 中添加常量“PHPUNIT_TESTING_SESSION”,如下所示:

<const name="PHPUNIT_TESTING_SESSION" value="true"/>
Run Code Online (Sandbox Code Playgroud)