简体   繁体   中英

Laravel 5 testing environment not getting set in Codeception unit tests

I'm using Laravel 5 and Codeception and I would like to use an in-memory SQLite database for my unit tests, however, I can't seem to get my environment set to "testing" in Codeception. I'm using the Laravel5 module and have the following defined in my unit.suite.yml file:

class_name: UnitTester
modules:
    enabled: [Asserts, UnitHelper, Laravel5]
    config:
        Laravel5:
            environment_file: .env.testing

I have a .env file which defines all my local settings, then a .env.testing file that defines all the testing-specific settings. However, it never seems to actually set the environment correctly.

To test the environment I just did:

$this->assertEquals('testing', \App::environment());

and I always get:

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'testing'
+'local'

Anyone have any idea what I'm doing wrong?

您是否在.env.testing文件中设置了环境名称?

APV_ENV=testing

This could be because you have the env file defined in your global codeception config. In codeception.yml file in your project root directory check the value for params, should be something like following

params:
    - .env.testing

I am completely new to laravel, but just found that the only way to get tests running on my local machine and (for example) Codeship, is to symlink .env when I need it. (I know - this isn't a very clean way, but hey, it works )

I have beside my local .env file:

#.env.testing
APP_ENV=testing
DB_CONNECTION=default_mysql
DB_DATABASE=test_db
... etc

And:

#.env.codeship
APP_ENV=codeship
DB_CONNECTION=codeship

For local tests I made an alias:

alias pl='rm .env; ln -s .env.testing .env; phpunit; rm .env; ln -s .env.local .env'

Where .env.local holds a copy of my local .env file.

And for Codeships test pipeline:

ln -s .env.codeship .env
php artisan migrate --seed
phpunit

Database configuration:

// CODESHIP
'codeship' => [
    'driver' => 'mysql',        
    'username' => getenv('MYSQL_USER'),
    'password' => getenv('MYSQL_PASSWORD'),
    // etc        
],

// LOCAL DEV // PHPUNIT
'default_mysql' => [
    'driver' => 'mysql',
    'username' => env('DB_USERNAME', 'localhost'),
    'password' => env('DB_PASSWORD', 'forge'),
    // etc        
],

If anybody got better ideas, I am happy to hear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM