Search code examples
phpunit-testingyii2codeception

How to run unit tests with Codeception in Yii2?


I'm new to the Codeception framework and trying to run the unit test scripts from this directory:

tests/codeception/frontend/unit

DB configuration for testing has been done in config-local.php.

Now, my question is how to run the test scripts? I've tried to run the following commands from the terminal:

frontend tests
cd frontend
codecept build
codecept run

But it says Codecept: command not found.


Solution

  • Install Codeception via composer:

    $ composer require "codeception/codeception"
    

    From now on Codeception (with installed PHPUnit) can be run as:

    $ php vendor/bin/codecept
    

    Next, initialize your testing environment:

    $ php vendor/bin/codecept bootstrap
    

    Finally, run the following commands from the yii2 tests/codeception/frontend folder:

    $ php vendor/bin/codecept build
    $ php vendor/bin/codecept run
    

    Follow this Quickstart Guide to read more about Codeception installation process.

    Important note: if you want codecept command to work from the command line:

    codecept bootstrap
    codecept run
    

    ... then you need to configure the $PATH variable properly.