Showing
1 changed file
with
45 additions
and
0 deletions
1 | +# 单元测试中使用测试数据库 # | ||
2 | +--- | ||
3 | + | ||
4 | +Laravel's application testing features are now so user friendly that Laravel developers simply no longer have any excuses for not writing automated tests. | ||
5 | + | ||
6 | +Among the many test-related conveniences offered to you is the ability to configure a test database which will be used as a repository for all data created while running your test suite. Laravel will automate the creation and destruction of the very same tables you created through migrations, for each and every test, ensuring a pristine test environment. You can even use the config/database.php and .env files to manage the test database in the very same fashion used to manage your development and production databases. To identify a new database for test purposes, open the **config/database.php**</span> file and add a new entry to the connections array: | ||
7 | + | ||
8 | + | ||
9 | +<font color='DarkSlateBlue'> | ||
10 | + | ||
11 | + 'connections' => [ | ||
12 | + ... | ||
13 | + 'mysql_test' => [ | ||
14 | + 'driver' => 'mysql', | ||
15 | + 'host' => env('DBT_HOST', 'localhost'), | ||
16 | + 'database' => env('DBT_DATABASE', 'forge'), | ||
17 | + 'username' => env('DBT_USERNAME', 'forge'), | ||
18 | + 'password' => env('DBT_PASSWORD', ''), | ||
19 | + 'charset' => 'utf8', | ||
20 | + 'collation' => 'utf8_unicode_ci', | ||
21 | + 'prefix' => '', | ||
22 | + 'strict' => false, | ||
23 | + ], | ||
24 | + ... | ||
25 | + ], | ||
26 | +</font> | ||
27 | + | ||
28 | + | ||
29 | +<p>This connection alias is called <code>mysql_test</code>, and it refers to several environment variables used to connect to the database (<code>DBT_HOST</code>, <code>DBT_DATABASE</code>, <code>DBT_USERNAME</code>, and <code>DBT_PASSWORD</code>). Place these environment variables in your <code>.env</code> file:</p> | ||
30 | + | ||
31 | + | ||
32 | +<pre><code>DBT_HOST=localhost | ||
33 | +DBT_DATABASE=test_larabrain_com | ||
34 | +DBT_USERNAME=larabrain_tester | ||
35 | +DBT_PASSWORD=secret</code></pre> | ||
36 | + | ||
37 | +<p>Finally, open the <code>phpunit.xml</code> file and add the following line to the <code>php</code> enclosure:</p> | ||
38 | + | ||
39 | +<pre><code><php> | ||
40 | +... | ||
41 | +<env name="DB_CONNECTION" value="mysql_test"/> | ||
42 | +... | ||
43 | +</php></code></pre> | ||
44 | + | ||
45 | +<p>With this configuration in place, Laravel will now know which database should be used when your tests <a href="http://laravel.com/docs/master/testing#working-with-databases">interact with a database</a>.</p> | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment