Skip to content
Michael Härtl edited this page Sep 9, 2015 · 2 revisions

We have included a basic test setup that gets you started with testing in no time. It uses another docker-compose.yml where the test infrastructure is defined.

Note: If you host more than one yii2-dockerized project on your host you should set the COMPOSE_PROJECT envirnoment variable to a unique name for each project, before you run any of the below commands. Otherwhise you will get conflicts as each test container will automatically be called like tests_test_1 due to the same tests/ directory name.

Setup

Before you run the tests for the first time, you have to set up the test DB:

cd tests/
docker-compose run --rm test ./yii migrate

Note: This may fail the first time because the DB is not up. In this case try again. If this doesn't help either, try to run docker-compose up in another terminal window first, then try the above command again.

Writing tests

To write tests, you simply create the respective classes in the acceptance, functional and unit directories in the tests/codeception folder. You may also have to provide Page classes for acceptance and functional tests in the _pages directory or add some fixtures in the fixtures directory.

We have included some simple examples that should help you to get started. For further details on how to write tests, please refer to the codeception and Yii 2 documentation.

Running tests

To run test you only need one simple command inside the tests/ directory:

docker-compose run --rm test

You can also specify a specific codecept command:

docker-compose run --rm test codecept run functional

Creating fixtures

Before you can generate fixtures, you have to provide the respective template files for yii2-faker in the codeception/templates directory. Then you can create fixtures with:

docker-compose run --rm test ./yii fixture/generate <tablename>

Directory structure

The many files in the tests/ directory can be overwhelming. So here's a summary of what each file and directory is used for.

  - tests
     - codeception/             the codeception/ directory
        - _output/              temporary outputs (gitignored)
        - _pages/               pages shared by acceptance and functional tests
        - acceptance/           acceptance tests
        - config/               Yii app configuration ...
           acceptance.php       ... for acceptance tests
           config.php           ... shared by all tests
           functional.php       ... for functional tests
           unit.php             ... for unit tests
        - fixtures/             fixtures for all tests
           - templates/         templates for yii2-faker
        - functional/           functional tests
        - unit/                 unit tests
        _bootstrap.php          bootstrap file for all tests (load env and Yii.php)
        acceptance.suite.yml    configuration for acceptance tester
        functional.suite.yml    configuration for functional tester
        unit.suite.yml          configuration for unit tester
     codeception.yml            main configuration for codeception
     docker-compose.yml         docker compose configuration for testing
     yii                        CLI for the testing environment (migrations, fixtures, ...)