Are you using Laravel 4 or Laravel 5?
Setting testing environment variables
Hi All,
I specify my environment variables in phpunit.xml and have created a .env.testing file.
However when I run:
php artisan migrate --env="testing"
I get:
Nothing to migrate
How I can I make Laravel load my test variables?
This is my setup!
I create a .env.testing file where I keep all my settings for my database stuff like the file below. Note that I didn't include the database credentials since I use sqlite for testing so you don't need that stuff (it's much faster then mysql)
.env.testing
APP_ENV=testing
APP_DEBUG=true
APP_KEY=TcqTXRLRsXVJeEgbiAgk8NwtEvQTiIbc
DB_DEFAULT=sqlite_testing
CACHE_DRIVER=file
SESSION_DRIVER=file
I updated my config/database.php file like this. Node that I specified tho sqlite_testing variables. You can either use a sqlite database or use memory for testing. Memory is even faster. I only use memory for unit testing since I want to test my functional tests and integration tests on a real database like sqlite or mysql.
config/database.php
'default' => env('DB_DEFAULT', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path().'/database.sqlite',
'prefix' => '',
],
'sqlite_testing' => [
'driver' => 'sqlite',
'database' => storage_path().'/testing.sqlite',
'prefix' => '',
],
'sqlite_unit_testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
// other connections
],
Then I updated my functional and unit tests suites.yml files like so
tests/functional.suite.yml
class_name: FunctionalTester
modules:
enabled: [Laravel5, FunctionalHelper, Asserts]
config:
Laravel5:
environment_file: .env.testing
tests/unit.suite.yml
class_name: UnitTester
modules:
enabled: [Asserts, UnitHelper, Laravel5]
config:
Laravel5:
environment_file: .env.unittesting
Now it's important that if you use the sqlite database you need to migrate it as well! Since we did set it up in our config it's really easy to migrate it. We first need to create a file called testing.sqlite in the storage directory. Now we can migrate that database, like we specified in the config
php artisan migrate --database=sqlite_testing
This should get you up and running
Please or to participate in this conversation.