Laravel 5:使用不同的数据库进行测试和本地 [英] Laravel 5 : Use different database for testing and local
问题描述
如何更改数据库以在本地系统上进行开发和测试,而无需每次都编辑.env
文件?
How does one change database for development and testing on local system without editing the .env
file each time?
因此,我发现练习 TDD 非常不方便.
I have found it quite inconvenient to practice TDD because of this.
Laravel 应用程序是否可以区分常规开发和测试,以便它可以选择适当的数据库?
Is it possible for a Laravel application to differentiate between normal development and testing so that it can choose the appropriate database?
推荐答案
在Laravel中创建测试数据库配置
编辑config\database.php
文件,并将testing
-数组添加到connections
数组中:
Create a testing database configuration in Laravel
Edit the config\database.php
file and add a testing
- array into the connections
array:
'connections' => [
'testing' => [
'driver' => env('DB_TEST_DRIVER'),
// more details on your testing database
]
]
然后将必要的变量添加到您的.env
文件中.
Then add the necessary variables to your .env
-file.
打开您的phpunit.xml
文件,并将以下内容添加到您的<php>
标签中:
Open your phpunit.xml
-file and add the following within your <php>
-tag:
<env name="DB_CONNECTION" value="testing"/>
现在,PHPUnit将在您在testing
-数组中定义的数据库上运行测试.
Now PHPUnit will run with the tests on the database you defined in the testing
- array.
这篇关于Laravel 5:使用不同的数据库进行测试和本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!