【问题标题】:change env when acceptance testing laravel app with codeception and selenium使用 codeception 和 selenium 对 laravel 应用程序进行验收测试时更改 env
【发布时间】:2014-11-26 02:15:33
【问题描述】:

我正在尝试使用 codeception 和 selenium 模块为 laravel 4 编写一些验收测试。

我有两个问题。

  • 第一个是我的应用正在运行 在宅基地 vagrant vm 和 selenium 服务器上运行 主机。那么有没有一种简单的方法可以在 vm 中运行 selenium 服务器并在主机上调用浏览器?

  • 我的第二个问题是,当测试实际使用实时数据库时,因为 laravel 应用程序的环境没有设置为测试。显然我想让它使用测试数据库并在每次测试后重置它。

codeception.yaml

actor: Tester
paths:
    tests: app/tests
    log: app/tests/_output
    data: app/tests/_data
    helpers: app/tests/_support
settings:
    bootstrap: _bootstrap.php
    colors: true
    memory_limit: 1024M
    suite_class: \PHPUnit_Framework_TestSuite
modules:
    config:
        Db:
            dsn: 'sqlite:app/tests/_data/testdb.sqlite'
            user: ''
            password: ''
            dump: app/tests/_data/dump.sql

acceptance.yaml

class_name: AcceptanceTester
modules:
    enabled: [WebDriver,AcceptanceHelper]
    config:
        WebDriver:
            url: 'http://app.dev'
            browser: firefox
            window_size: 1920x1024
            wait: 10 

【问题讨论】:

    标签: selenium laravel-4 acceptance-testing codeception


    【解决方案1】:

    在 vm 中运行验收测试的简单方法是在 ghostdriver 模式下使用 phantom js。这里有教程:

    https://gist.github.com/antonioribeiro/96ce9675e5660c317bcc

    当测试失败时,你仍然可以看到截图和渲染的 html,所以你看不到浏览器并不是什么大问题。

    对于您的第二个问题,我更喜欢使用自己的数据库进行单独的测试安装。这样,您在 dev 中所做的更改不会改变您的测试结果,而且它更接近于生产。

    如果您想使用相同的安装,您可以使用 .env 文件自动切换设置。

    http://laravel.com/docs/4.2/configuration#protecting-sensitive-configuration

    你的配置应该是这样的:

    'host'     => $_ENV['DB_HOST'],
    'database' => $_ENV['DB_NAME'],
    'username' => $_ENV['DB_USERNAME'],
    'password' => $_ENV['DB_PASSWORD'],
    

    你的 .env.php 看起来像:

    return array( 'DB_HOST' => 'hostname', 'DB_NAME' => '' ..etc
    

    您可以使用像 robo 这样的任务运行器来自动更新您的 .env 文件并运行代码接收测试。

    http://robo.li/

    $this->replaceInFile('.env.php')
        ->from('production_db_name')
        ->to('test_db_name')
         ->run();
    
    $this->taskCodecept()->suite('acceptance')->run();
    

    .env 文件在 Laravel 5 中发生了变化,但这个工作流程仍然可以进行最少的修改。

    http://mattstauffer.co/blog/laravel-5.0-environment-detection-and-environment-variables

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      • 2015-07-21
      • 2017-03-25
      • 1970-01-01
      相关资源
      最近更新 更多