本文介绍了为laravel应用运行phpunit测试时忽略.env.testing文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


Laravel版本:"laravel/framework": "5.8.*",

我拥有标准的.env文件

APP_ENV=local
DB_CONNECTION=mysql

我已经克隆了标准的.env文件并另存为.env.testing,然后更改为

I've cloned the standard .env file and saved as .env.testing, then changed to

APP_ENV=testing
DB_CONNECTION=mongodb

从控制台,从项目根目录内部,我正在尝试使用

From console, from inside the root of the project, I'm trying to run test using

php artisan config:cache && vendor/bin/phpunit

问题在于Laravel/PHPUnit仍在使用.env文件,而不是预期的.env.testing.这就是证明

The problem is that Laravel/PHPUnit is still using the .env file, instead of the expected .env.testing. This is the proof

这是实际的phpunit.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>

        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="MAIL_DRIVER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
    </php>
</phpunit>

所以,请提出调试和修复的建议

So, I kindly ask a suggestion for debugging and fixing

推荐答案

我发现了这个问题: https://github.com/sebastianbergmann/phpunit/issues/2353

因此,我尝试更改.xml文件的这一部分以强制执行我的配置.

So I tried to change this portion of .xml file to force my configs.

有效.请注意,所有行现在都附加了force="true"

It works. note all rows are now with appended force="true"

    <server name="APP_ENV" value="testing"  force="true"/>
    <server name="BCRYPT_ROUNDS" value="4" force="true"/>
    <server name="CACHE_DRIVER" value="array" force="true"/>
    <server name="MAIL_DRIVER" value="array" force="true"/>
    <server name="QUEUE_CONNECTION" value="sync" force="true"/>
    <server name="DB_CONNECTION" value="mongodb_testing" force="true"/>
    <server name="SESSION_DRIVER" value="array" force="true"/>

这篇关于为laravel应用运行phpunit测试时忽略.env.testing文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 02:13