Laravel 测试后重置数据库

Jen*_*hou 3 sql testing laravel laravel-5 laravel-dusk

我刚刚开始使用 Laravel Dusk 来测试我的项目,需要一些指导。在我运行所有可用的测试之后,我希望能够在运行测试之前将我的数据库重置回。(如果在我运行测试之前我的数据库中有任何条目,我仍然希望在运行测试后看到它们。但是,在测试期间创建的任何条目,我不希望在测试运行后看到它们。 ) 关于我如何实现这一目标的任何指示?谢谢!

更新:

<?php

namespace Tests\Browser;

use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class UserRegisterTest extends DuskTestCase
{
    use DatabaseTransactions;
    /**
     * A test for user registration.
     * @group register
     * @return void
     */

    public function testRegisterUser()
    {
        //Register with all info filled out correctly
        $this->browse(function ($browser){
            $browser->visit('/register')
                    ->type('firstName', 'JenLogin')
                    ->type('lastName', 'Zhou')
                    ->type('email', 'testLogin@gmail.com')
                    ->type('bio', 'Hello, this user is for testing login purposes!')
                    ->type('location_zip', '11111')
                    ->type('password', '123456')
                    ->type('password_confirmation', '123456')
                    ->click('.btn-primary')
                    ->assertPathIs('/home')
                    ->click('.dropdown-toggle')
                    ->click('.dropdown-menu li:last-child');

        });

        $this->assertDatabaseHas('users', ['firstName' => 'JenLogin', 'lastName' => 'Zhou', 'email' => 'testLogin@gmail.com']);


    }

    /**
     * Register with duplicate user
     * @group register
     * @return void
     */
    public function testRegisterDuplicateUser(){

        $this->browse(function ($browser){
            $browser->visit('/register')
                ->type('firstName', 'JenLoginDup')
                ->type('lastName', 'Zhou')
                ->type('email', 'testLogin@gmail.com')
                ->type('bio', 'Hello, this user is for testing login purposes!')
                ->type('location_zip', '11111')
                ->type('password', '123456')
                ->type('password_confirmation', '123456')
                ->click('.btn-primary')
                ->assertPathIs('/register')
                ->assertSee('The email has already been taken.');
        });

        $this->assertDatabaseMissing('users', ['firstName' => 'JenLoginDup', 'lastName' => 'Zhou', 'email' => 'testLogin@gmail.com']);
    }

    /**
     * Register with incorrect password confirmation
     * @group register
     * @return void
     */
    public function testRegisterUserNoPassConfirm(){

        $this->browse(function ($browser){
            $browser->visit('/register')
                ->type('firstName', 'JenLoginPass')
                ->type('lastName', 'Zhou')
                ->type('email', 'testLoginPass@gmail.com')
                ->type('bio', 'Hello, this user is for testing login purposes!')
                ->type('location_zip', '11111')
                ->type('password', '123456')
                ->type('password_confirmation', '888888')
                ->click('.btn-primary')
                ->assertPathIs('/register')
                ->assertSee('The password confirmation does not match.');
        });

        $this->assertDatabaseMissing('users', ['firstName' => 'JenLoginPass', 'lastName' => 'Zhou', 'email' => 'testLoginPass@gmail.com']);
    }
}
Run Code Online (Sandbox Code Playgroud)

whe*_*ker 12

您正在寻找 DatabaseTransactions 特征。像这样在您的测试类中使用它,它将自动回滚您在测试期间所做的所有数据库事务。

use Illuminate\Foundation\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
    use DatabaseTransactions;

    // test methods here
}
Run Code Online (Sandbox Code Playgroud)

这将跟踪测试期间所做的所有事务,并在完成后撤消它们。

注意:此特性仅适用于默认数据库连接


Mar*_*łek 6

首先,当您运行测试时,您应该使用与实时(或开发)数据库完全不同的数据库。为此,您应该.env.dusk在其中创建并设置:

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=testing_database
DB_USERNAME=root
DB_PASSWORD=pass
Run Code Online (Sandbox Code Playgroud)

仅用于测试的数据库。

第二件事是,对于 Laravel Dusk 你不能只使用DatabaseTransactions. 事实上你应该使用DatabaseMigrationsDusk 测试,否则你会得到意想不到的结果。


Bar*_*art 3

没有健全的工作流程来在实时/开发数据库上运行数据测试并通过测试恢复更改。

因此你的方法在这里失败了,你应该:

  1. 为测试创建单独的测试模式/数据库
  2. 在运行测试之前切换到测试数据库 - 这可以以某种方式自动化,具体取决于您在 phpunit 和 .env.dusk 中的配置,但这取决于您的本地设置。
  3. 然后在您的测试中,您将在干净的数据库上从头开始创建所有内容(运行迁移、种子、工厂)
  4. 针对此测试数据库运行测试
  5. 对于开发,使用当前数据切换回基础数据库,这不会受到测试的影响。

下次您运行测试时,所有测试都会从零开始 - 清理数据库,这将通过测试完成: use CreatesApplication; use DatabaseMigrations; parent::setUp();等。

阅读有关这些方法的更多信息...

附注:

  1. 通过这种方法,在 CI 环境中测试您的应用程序也将变得很容易。

  2. 切勿编写依赖于开发/实时数据库上的数据的测试。对于测试,所有必需的数据应由种子或最终工厂提供!