skip to Main Content

Making tests ("phpunit/phpunit": "^9.5.10") for laravel ("laravel/framework": "^9.2") site I want to use .env.testing file

But I got error running command :

$ vendor/bin/phpunit tests/Feature/PagesCrudTest.php     --env=testing

Unknown option "--env"

Checking help I did not find any "env" issue mentioned:

$ vendor/bin/phpunit tests/Feature/PagesCrudTest.php  --help
...

My phpunit.xml :

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
         printerClass="SemproPHPUnitPrettyPrinterPrettyPrinterForPhpUnit9"
>
    <testsuites>
        <testsuite name="Pages CRUD tests">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </coverage>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="TELESCOPE_ENABLED" value="true"/>

        <!--         <env name="DB_CONNECTION" value="sqlite"/>-->
        <!--         <env name="DB_DATABASE" value=":memory:"/>-->
        <env name="MAIL_MAILER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

In which way can I use .env.testing in my tests ?

Updated Info

  1. I have in phpunit.xml line:

    env name="APP_ENV" value="testing"

I suppose this file has valid structure/data

  1. Running command

    vendor/bin/phpunit tests/Feature/PagesCrudTest.php

I make checks of current database name using DB::connection()->getDatabaseName() in the beginning of the tests
and I see that is is from .env file.

  1. So I try with option

    tests –env=testing

I tried to fill option

--env=testing

manually to avoid misspeling when copypasting from browser – but I still got

Unknown option "--env"

error…

Updated Info # 2
after some test it looks like I need to clear config cache firstly :

php artisan config:clear
vendor/bin/phpunit tests/Feature/PagesCrudTest.php

and it works without any “–env=testing” option in command (I found this as possible decision in net).
Not sure is it good decision. Now I work locally at my ubuntu 20.

But how can I run tests on live server ?

Updated Info # 3

If to run

php artisan config:clear

before tests I got error on any test in my file

...
12) TestsFeaturePagesCrudTest::testGetFilteredOnlyIsHomepage
IlluminateContractsContainerBindingResolutionException: Target class [env] does not exist.

ProjectPath/vendor/laravel/framework/src/Illuminate/Container/Container.php:877
ProjectPath/vendor/laravel/framework/src/Illuminate/Container/Container.php:756
ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:857
ProjectPath/vendor/laravel/framework/src/Illuminate/Container/Container.php:692
ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:842
ProjectPath/vendor/laravel/framework/src/Illuminate/Container/Container.php:1415
ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:575
ProjectPath/app/Providers/TelescopeServiceProvider.php:24
ProjectPath/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:60
ProjectPath/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:60
ProjectPath/vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php:299
ProjectPath/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:61
ProjectPath/vendor/laravel/telescope/src/Telescope.php:326
ProjectPath/vendor/laravel/telescope/src/Telescope.php:284
ProjectPath/vendor/laravel/telescope/src/Telescope.php:333
ProjectPath/vendor/laravel/telescope/src/Telescope.php:445
ProjectPath/vendor/laravel/telescope/src/Watchers/LogWatcher.php:41
ProjectPath/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:421
ProjectPath/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:249
ProjectPath/vendor/laravel/framework/src/Illuminate/Log/Logger.php:245
ProjectPath/vendor/laravel/framework/src/Illuminate/Log/Logger.php:186
ProjectPath/vendor/laravel/framework/src/Illuminate/Log/Logger.php:130
ProjectPath/vendor/laravel/framework/src/Illuminate/Log/LogManager.php:677
ProjectPath/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:338
ProjectPath/app/Providers/RouteServiceProvider.php:50
ProjectPath/app/Providers/RouteServiceProvider.php:29
ProjectPath/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
ProjectPath/vendor/laravel/framework/src/Illuminate/Container/Util.php:41
ProjectPath/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
ProjectPath/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
ProjectPath/vendor/laravel/framework/src/Illuminate/Container/Container.php:651
ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:930
ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:911
ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:912
ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php:17
ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:239
ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:310
ProjectPath/tests/CreatesApplication.php:18
ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:110
ProjectPath/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:87
ProjectPath/tests/Feature/PagesCrudTest.php:25

ERRORS!
Tests: 12, Assertions: 0, Errors: 12.

But all the rests are running ok
I suppose that it is related that that now .env.testing is used, but how can it be fixed?

Thanks in advance!

2

Answers


  1. There’s no need for --env=testing. The phpunit.xml already sets the APP_ENV to testing with <env name="APP_ENV" value="testing"/>

    In addition, you may create a .env.testing file in the root of your project. This file will be used instead of the .env file when running PHPUnit tests or executing Artisan commands with the –env=testing option.

    So it will be used by PHPUnit tests and you can use it with other artisan commands as well with --env=testing

    Login or Signup to reply.
  2. You get the error because you pass --env=testing which is an unknown PHPUnit option. If you want to set the environment for testing, you have several options depending on your application. Remember, the application environment doesn’t really have anything to do with PHPUnit.

    If your application requires an environment variable, e.g. APP_ENV, then you might be able to set it on the command line at runtime:

    $ APP_ENV=testing vendor/bin/phpunit tests/Feature/PagesCrudTest.php
    

    If you don’t want to have to set it every time you run a test then you might be able to set in the PHPUnit configuration file:

        <php>
            <server name="APP_ENV" value="testing" />
        </php>
    

    See the PHPUnit configuration documentation for more details.

    On the other hand, if your application uses PHP dotenv to handle environment configuration then you have even more options, for example in a testing specific dotenv file:

    .env.testing

    APP_ENV=testing
    

    It all depends on your application. You indicate that you’re using Laravel. Read Laravel Environment Configuration and Laravel Testing: Getting Started.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search