So far, we've seen two arguments of the codecept
command:
build
: This is used for building the "testers" and any additional code when using any additional modulerun
: This is used to execute the testsThere are a few parameters you can invoke run
with that I'd like to bring to your attention, as these will come handy when running and debugging the tests. The syntax of the run
command is as follows:
$ vendor/bin/codecept run [options] [suite] [test]
First of all, you can run a specific suite, such as unit, acceptance, or functional, or be more specific and run a single test file, for example:
$ ../vendor/bin/codecept run acceptance LoginCept.php … Time: 3.35 seconds, Memory: 13.75Mb OK (1 test, 5 assertions)
In the preceding command, you can also use the --steps
option, which is a way to be more verbose showing all the single steps taken by your tests while running them.
Alternatively, you also have --debug
, which will not only show the steps taken by your application, but also display what's happening behind the scenes, such as the POST request of data to a specific URL, the loading of a page, or the list of cookies set.
While running tests and seeing them passing will be all you care about once you've written your tests, you first need to write them.
Codeception helps us get started by providing a code generation argument on the command line:
generate:cept
: This is used for generating CEPT testsgenerate:cest
: This is used for generating CEST testsgenerate:phpunit
: This is used for generating PHPUnit tests, without the Codeception additionsgenerate:test
: This is used for generating unit testsAll the preceding arguments will require as parameters the suite name and the name of the file to create:
$ ../vendor/bin/codecept generate:cept acceptance ModalLoginCept Test was created in ModalLoginCept.php
You can review these and more commands by running codecept
without arguments.
One of the things that I have found particularly handy and that we're going to use extensively is the ability to run the same migrations that we will create for our application on your test database.
Migrations are a concept that is not exclusive to Yii, and you can read more about it in the documentation at http://www.yiiframework.com/doc-2.0/guide-db-migrations.html.
In the /tests/codeception/bin/
folder, you will find the yii
CLI command line that you can use against the test database you've configured previously to run the migrations.
Assuming you're in the root of your project, the following sequence of commands will show you how to run the migrations:
$ cd tests/codeception $ php bin/yii migrate/up Yii Migration Tool (based on Yii v2.0.0-dev) Creating migration history table "migration"...done. No new migration found. Your system is up-to-date.
The yii
CLI is exactly the same as the main one residing in the root of your project, with the only difference that it will read the test configuration, particularly the one regarding the database.