Installing and using Codeception in Yii 2

Yii comes with its own Codeception extension that provides a base class for unit tests (yiicodeceptionTestCase), a class for tests that require database interaction (yiicodeceptionDbTestCase), and a base class for Codeception page objects (yiicodeceptionBasePage).

As usual, our preferred method is using Composer:

$ composer require "codeception/codeception: 2.0.*" --prefer-dist --dev

There’s a specific reason to use –prefer-dist; if you’re using Git, you can get into a hairy situation with Git submodules (but again excluding the /vendor folder should solve most of these problems). To avoid repeating it every time we use Composer, just add the following to your composer.json file:

// composer.json

    "config": {
        "preferred-install": "dist"

Also, remember that using composer install will not work if you’ve added the component manually to your composer.json file as it would consider it a mismatch and raise an error. To install the package, you need to run composer update either for all the packages you have installed, or specifically with this:

$ composer update codeception/codeception

As highlighted before, you might also be interested in two additional packages codeception/specify and codeception/verify. These two packages provide a further level of abstraction that allows you to write more human readable tests by using a business-oriented syntax, close to what BDD definitions will look like.

Your composer.json file will contain the following:

// composer.json

    "require-dev": {
        "yiisoft/yii2-codeception": "*",
        "yiisoft/yii2-debug": "*",
        "yiisoft/yii2-gii": "*",
        "codeception/codeception": "2.0.*",
        "codeception/specify": "*",
        "codeception/verify": "*"

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest


Get the best in web dev

Join and recieve best in web dev , once a week FREE

An email has been Sent to your Inbox ! Please Confirm your Subscription :)