This article mainly introduces about using another method to carry out the Laravel database Test (SQLite), has a certain reference value, now share to everyone, the need for friends can refer to
Laravel Database Testing
In terms of testing, Laravel
built-in use PHPUnit
provides a very convenient solution. And for the database additions and deletions to the test, to solve a very important problem is how to restore the original database after testing, for example, to test a user registration method, you need to insert a user record to the database, but after the test is complete, we do not want this test case to be saved in the database. To solve this problem, a Laravel
very convenient solution is provided:
Reference: Https://laravel.com/docs/5.3/database-testing#resetting-the-database-after-each-test
Another solution: SQLite
the In-memory database used:memory:
Laravel
Two solutions provided, still read and write to the database, sometimes you may not want to do this (for example, multiple people sharing a online development database), at this time, but also in a more elegant way: SQLlite
, logic is actually very simple: when running the test case, the connection of the database is replaced with SQLite
.
Using the example
For example, we have the following test classes (which are not representative and are used only to illustrate the problem and assume that the machine is already installed SQLite
):
Class Homepagetest extends TestCase {public function testhomepage () { //create a test user and save $user = Factory ( App\user::class)->create (); $this->actingas ($user)->visit (' Home ')->see (' Dashboard ');} }
First Laravel
, config/database.php
connections
Add a new database connection in the database configuration file, that is, the array
' SQLite ' = [ ' driver ' + ' SQLite ', ' database ' = ': Memory: ', ' prefix ' = ', ',],
A very important parameter here is 'database' => ':memory:'
that the :memory:
database is SQLite
a built-in memory database, each time you run the test case will create a new database in memory, and after the test is completed to close the database connection, automatically cleared out, with good isolation, And because it's in memory, so fast, these features are very handy for testing, which is SQLite
one of the most important reasons we use a database as a test library. For a detailed explanation, click here.
<php> <env name= "app_env" value= "testing"/> <env name= "cache_driver" value= "Array"/> <env name= "session_driver" value= "array"/> <env name= "queue_driver" value= "Sync"/> <!- -Change the database connection to the SQLite connection just defined-- <env name= "db_connection" value= "SQLite"/></php>
This overrides the .env
database connection defined in DB_CONNECTION=mysql
and tells the framework to use the connection instead of the connection when it runs the test sqlite
mysql
.
Public Function SetUp () { parent::setup (); Perform a database migration $this->artisan (' Migrate ');}
This will perform all migrations to the database before each execution of the test case, SQLite
:memory:
generating the data tables needed for the business logic.
Solution Pros and cons
The key point of this scheme is to use SQLite
a built-in memory database, so it is faster :memory:
, has good isolation, and does not have any impact on our development database.
Of course, the program also has shortcomings, if the project database is large, there are a large number of data tables or migration files, will consume a lot of memory, when running the test may be due to insufficient memory, resulting in test interruption. At this point, you need to allocate the appropriate memory for PHP, modify the configuration in the php.ini
memory_limit = 128M
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!