Another way to perform Laravel database testing (SQLite)

Source: Internet
Author: User
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:

    • Use migration:DatabaseMigrations

    • Using transactions:DatabaseTransactions

Reference: Https://laravel.com/docs/5.3/database-testing#resetting-the-database-after-each-test

Another solution: SQLite the In-memory database used:memory:

LaravelTwo 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.

    • Then you need to modify PHPUnit the configuration file in phpunit.xml , change the database connection to the connection you just defined SQLite

<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 .

    • Finally, you need to perform a database migration before running the test case, TestCase.php adding methods in the base class of the Test class setUp

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.inimemory_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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.