Yii2 Advanced Application uses Codeception as its primary test framework.
There are already some sample tests prepared in
tests directory of
In order for the following procedure to work, it is assumed that the application has been initialized using
dev environment. In the case where tests need to be executed in a
yii_test.bat must be manually copied from the
environments/dev folder into the project root directory.
Tests require an additional database, which will be cleaned up between tests. This database will be used to hold data that matches the data in your live database. Its purpose is to run those tests that store data without affecting your own data. Create database in your database server, using a similar setup to the live database, so that the tests are run in an environment that is as close to your live environment as possible.
common/config/main-local.php. It is something like
'dsn' => 'mysql:host=localhost;dbname=yii2advanced'. Copy it to
common/config/test-local.phpand change the
dbnameso it is obvious that it is the test database:
'dsn' => 'mysql:host=localhost;dbname=yii2advanced_test'.
yii2advanced_testin MySql (according to config in
Then all sample tests can be started by running:
You will see output similar to this:
It is recommended to keep your tests up to date. If a class, or functionality is deleted, corresponding tests should be deleted as well. You should run tests regularly, or better to set up Continuous Integration server for them.
Please refer to Yii2 Framework Case Study to learn how to configure Codeception for your application.
Tests for common classes are located in
common/tests. In this template there are only
Execute them by running:
vendor/bin/codecept run -- -c common
-c option allows to set path to
unit test suite (located in
common/tests/unit) can use Yii framework features:
Yii::$app, Active Record, fixtures, etc.
This is done because
Yii2 module is enabled in unit tests config:
common/tests/unit.suite.yml. You can disable it to run tests in complete isolation.
Frontend tests contain unit tests, functional tests, and acceptance tests. Execute them by running:
vendor/bin/codecept run -- -c frontend
Description of test suites:
unit⇒ classes related to frontend application only.
functional⇒ application internal requests/responses (without a web server).
By default acceptance tests are disabled, to run them use:
To execute acceptance tests do the following:
frontend/tests/acceptance.suite.yml to enable suite configuration
codeception/base package in
codeception/codeception to install full featured
version of Codeception
Update dependencies with Composer
Auto-generate new support classes for acceptance tests:
vendor/bin/codecept build -- -c frontend
Download Selenium Server and launch it:
java -jar ~/selenium-server-standalone-x.xx.x.jar
> There are currently issues with geckodriver's
> interactions with selenium that require you to enable the protocol translating in Selenium.
java -jar ~/selenium-server-standalone-x.xx.x.jar -enablePassThrough false
Start web server:
php -S 127.0.0.1:8080 -t frontend/web
Now you can run all available tests
vendor/bin/codecept run acceptance -- -c frontend
Backend application contain unit and functional test suites. Execute them by running:
vendor/bin/codecept run -- -c backend