Tests Overview
Tests Init Flow
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.
-
Your live database connection string is probably in
common/config/main-local.php. It is something like'dsn' => 'mysql:host=localhost;dbname=yii2advanced'. Copy it tocommon/config/test-local.phpand change thedbnameso it is obvious that it is the test database:'dsn' => 'mysql:host=localhost;dbname=your_test_db_name'. -
Create an empty database. In this example it would be
your_test_db_namein MySql (according to config incommon/config/test-local.php). -
Execute:
./yii_test migrate(yii_testandyii_test.batmust be manually copied from theenvironments/devfolder into the project root directory.) -
Build the test suite:
./vendor/bin/codecept build -
API tests are not included in any Yii templates so you need to set up them manually if you developing a web service. To do it execute
./vendor/bin/codecept g:suite api -c api. -
You will need to enable
REST,Yii2module inapi/tests/api.suite.yml:actor: ApiTestermodules:enabled:- \api\tests\Helper\Api- REST:#url:depends: Yii2#part: Json
Then all sample tests can be started by running: ./vendor/bin/codecept run.