设置测试数据库以进行Codeception(Laravel)中的验收测试 [英] Setup testing database for acceptance tests in Codeception (Laravel)

查看:125
本文介绍了设置测试数据库以进行Codeception(Laravel)中的验收测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Codeception框架在Laravel 5应用程序中执行验收测试.我想使用一个单独的数据库进行测试,以防止实际数据库在测试过程中发生变化.我根据Codeception文档配置了.yml文件.但是,无论如何,真实的数据库都会受到影响.这是我的配置文件:

I'm using Codeception framework to perform acceptance tests in Laravel 5 application. I wanted to use a separate database for testing to keep real database from changing through testing. I configued the .yml files based on Codeception documentation. But the real database gets affected anyway. This is my configuration files:

/codeception.yml

/codeception.yml

actor: Tester
 paths:
   tests: tests
    log: tests/_output
    data: tests/_data
   support: tests/_support
   envs: tests/_envs
 settings:
   bootstrap: _bootstrap.php
   colors: true
    memory_limit: 1024M
  extensions:
  enabled:
    - Codeception\Extension\RunFailed
  modules:
  config:
     Db:
        dsn: 'mysql:host=localhost;dbname=realDB'
        user: 'root'
        password: 'secret'
        dump: 'tests/_data/dump.sql'

/tests/acceptance.suite.yml

/tests/acceptance.suite.yml

class_name: AcceptanceTester
modules:
  enabled:
    - WebDriver:
        url: 'http://localhost:8000/'
        browser: firefox
    - Laravel5:
        part: ORM
        cleanup: false # can't wrap into transaction
     Db:
      populate: true
      cleanup: true
    - \Helper\Acceptance

realDB是真实的数据库,它在执行验收测试后会更改.我在accepting.suite.yml中尝试了不同的清理情况:1)清理:在Laravel模块中为false,清理:在Db模块中为true 2)清理:在Laravel模块中为true,而清理:在Db模块中为true. Codeception文档说,对于验收测试,我们需要禁用清理并使用Db模块清理测试之间的数据库".但是无论如何,realDB都会发生变化.

The realDB is the real database and it changes after performing acceptance test. I tried different cases for cleanup in acceptance.suite.yml: 1) cleanup: false in Laravel module and cleanup: true in Db module 2) cleanup: true in Laravel module and cleanup: true in Db module. Codeception documentation says that for acceptance test, we need to "disable cleanup and use Db module to cleanup database betweem tests". But the realDB changes anyway.

我已经为PHP尝试了不同的测试框架,例如Laravel中的PHPUnit,PHP Selenium驱动程序,Facebook Web驱动程序,并且在执行验收测试时,在所有情况下,真实数据库都会受到影响.如何正确配置Codeception以防止数据库更改?

I've tried different testing frameworks for PHP such as PHPUnit in Laravel, PHP Selenium driver, Facebook web driver and in all cases the real database get affected when performing acceptance tests. How can I correctly configure Codeception to prevent database changes?

任何帮助将不胜感激.

Any help would be appreciated.

[UPDATE1]

由于@TheFallen建议我使用其他数据库进行测试,因此我更改了配置文件,如下所示:

As @TheFallen suggested that I use different database for testing, I change the configuration files like this:

/codeception.yml

/codeception.yml

actor: Tester
 paths:
   tests: tests
    log: tests/_output
    data: tests/_data
   support: tests/_support
   envs: tests/_envs
 settings:
   bootstrap: _bootstrap.php
   colors: true
    memory_limit: 1024M
  extensions:
  enabled:
    - Codeception\Extension\RunFailed
  modules:
  config:
     Db:
        dsn: 'mysql:host=localhost;dbname=testDB'
        user: 'root'
        password: 'secret'
        dump: 'tests/_data/dump.sql'

/tests/acceptance.suite.yml

/tests/acceptance.suite.yml

class_name: AcceptanceTester
modules:
  enabled:
    - WebDriver:
        url: 'http://localhost:8000/'
        browser: firefox
    - Laravel5:
        part: ORM
        environment_file: .env.testing
        cleanup: true
     Db:
      populate: true
      cleanup: true
    - \Helper\Acceptance 

/.env.testing

/.env.testing

APPLICATION_URL=http://localhost:8000
APP_DEBUG=true
APP_ENV = testing
MYSQL_MAIN_HOST=localhost
MYSQL_MAIN_DATABASE=realDB
MYSQL_MAIN_USER=root
MYSQL_MAIN_PASSWORD=secret 
CACHE_DRIVER=array 
DB_CONNECTION=test_mysql
TEST_MYSQL_MAIN_DATABASE=testDB

/config/database.php

/config/database.php

return [

    'fetch' => PDO::FETCH_CLASS,

    'default' => env('DB_CONNECTION', 'mysql'),

    'connections' => [

        'test_mysql' => [
            'driver'    => 'mysql',
            'host'      => env('MYSQL_MAIN_HOST', 'localhost'),
            'database'  => env('TEST_MYSQL_MAIN_DATABASE', 'testDB'),
            'username' => env('MYSQL_MAIN_USER', 'root'),
            'password' => env('MYSQL_MAIN_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],
...

.env.testing位于Laravel应用程序的根目录中. 但是真实数据库(realDB)仍然会受到影响,只有转储文件会导入测试数据库(testDB)中.看来应用程序没有使用.env.testing.我怎样才能解决这个问题?

The .env.testing is located in the root of Laravel application. But real database (realDB) still gets affected and only dump file get imported in testing database (testDB). It looks like application doesn't use .env.testing. How can I fix this?

请注意,在我的验收测试中,发送了AJAX请求以调用更改数据库中数据的函数.我想回滚由AJAX请求完成的数据库事务.

Please note that, an AJAX request is sent to call a function changing data in database in my acceptance test. I want to rollback database transactions done by AJAX requests.

[UPDATE2]

根据编解码文档,验收测试将在开发环境中使用真实的Web服务器执行,因此设置无法从.env.testing传递给他们. :(

According to Codeception documentation, Acceptance tests will be executed in development environment using real web server, so settings from .env.testing can’t be passed to them. :(

为解决这个问题而做的所有事情之后,我得出结论,除非执行更改默认数据库以在.env中测试一个默认数据库,否则在执行验收测试触发AJAX请求执行数据库事务之后,无法阻止真实数据库的更改.文件!

After all that I've done to solve this problem, I conclude that it is impossible to keep real database from changing after performing acceptance tests triggering AJAX requests to perform database transactions unless I change the default database to testing one in .env file!

如果有人有更好的解决方案,请分享!

If anyone has a better solution, please share it!

推荐答案

由于您的Laravel应用程序可以在多个域中运行,因此请创建一个专用于接受测试的域,并将服务器配置为设置环境变量APP_ENV=acceptance(或其他任何设置)称呼此特定域).设置APP_ENV时,Laravel将自动为您加载正确的环境文件,在这种情况下为.env.acceptance.

Since your Laravel app can run under multiple domains, create one dedicated for your acceptance tests and configure the server to set the environment variable APP_ENV=acceptance (or whatever you call it) for this particular domain. When APP_ENV is set Laravel will automatically load the right environment file for you, in this case .env.acceptance.

我还看到人们使用cookie(在验收测试的早期设置)来配置Laravel以基于这些cookie切换数据库.似乎有点肮脏并且可能不安全,而且您最终将代码添加到了应用程序核心中,所以...

I've also seen folks using cookies (set early in the acceptance tests) to configure Laravel to switch databases based on those cookies. It seems a little dirty and potentially insecure, plus you end up adding code to your application core so...

这篇关于设置测试数据库以进行Codeception(Laravel)中的验收测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆