Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are acceptance tests in Codeception supposed to run in testing environment? (Laravel4 + Codeception)

I am trying to run some acceptance tests in my Laravel application. While functional tests trigger testing environment, acceptance tests do not. Is it a bug or a feature of acceptance tests? The main problem why this is bothering me is the fact, that it is not using(+populating+cleanup) testing database, it only connects to dev database (which is used, when no other ENV is specified e.g. testing, production) and this often fails those tests when I run them multiple times.

This is my configuration:

codeception.yml

paths:
    tests: app/tests
    log: app/tests/_log
    data: app/tests/_data
    helpers: app/tests/_helpers
settings:
    bootstrap: _bootstrap.php
    suite_class: \PHPUnit_Framework_TestSuite
    colors: true
    memory_limit: 1024M
    log: true
modules:
    config:
        Db:
            dsn: 'mysql:host=localhost;dbname=testdb'
            user: 'root'
            password: 'root'
            dump: 'app/tests/_data/dump.sql'
            populate: true
            cleanup: true

acceptance.suite.yml

class_name: WebGuy
modules:
    enabled:
        - PhpBrowser
        - WebHelper
        - Db
    config:
        PhpBrowser:
            url: 'http://localhost/'

functional.suite.yml

class_name: TestGuy
modules:
    enabled: [Filesystem, TestHelper, Laravel4, Db]

Thanks for your help!

like image 462
HRcc Avatar asked Feb 19 '14 01:02

HRcc


People also ask

What is Codeception?

Codeception is a framework used for creating tests, including unit tests, functional tests, and acceptance tests. Despite the fact that it is based on PHP, the user needs only basic knowledge for starting work with the framework, thanks to the set of custom commands offered by Codeception.


1 Answers

"Acceptance" tests are not run in the testing environment. The reason is when Laravel is in the testing environment, it disables filters by default. So therefore the testing environment is only for unit and functional tests.

Acceptance tests should be run in another environment (like dev or a specific one to Codeception).

Because Codeception 2.x now uses Guzzle to get a page response, it is possible to detect when you are on your host machine and Codeception is doing a specific request. That way you can have a "testing" environment and also a "codeception" environment specifically for your acceptance tests.

If you are using Homestead, I do this in my start.php file to detect if Codeception is running, and specifically put it into a 'codeception' environment, otherwise I let it run my environment detection normally

if ((gethostname() === 'homestead') && (isset($_SERVER['REMOTE_ADDR'])) && ($_SERVER['REMOTE_ADDR'] === '127.0.0.1'))
{
    $env = $app->detectEnvironment(['codeception' => ['homestead']]);
}
else
{
    $env = $app->detectEnvironment(['dev' => ['homestead']]);
}

Then in my 'codeception' environment, I setup a SQLite file database, and run acceptance tests against that (which is faster than mySQL testing).

like image 132
Laurence Avatar answered Nov 14 '22 00:11

Laurence