How do I setup API testing with Codeception and Yii2?

I am creating an API in Yii2 and have unit testing with Codeception working as expected. However I am unable to get API testing to work. I see that Codeception has API testing built-in but unsure how to get that working in Yii2.

I have run codecept bootstrap but it only built unit, functional and acceptance files/folders. There is no methods such as sendPost(), sendGet() etc to be found. Following examples in the documentation I thought I could run codecept generate:suite api to build the API part but still no good.

Does anyone have any suggestions on what I can try?

You have to create a new test suite:

If you have done this and it still doesn´t work you maybe forgot to run the build command?!

Thanks for the response. I have tried everything but maybe not in the right order. I expected the generate:suite to build tests/api/ApiGuy.php but it. However running the build afterwards did build tests/api/NoGuy.php which has the methods that I need.


Check that in functional.suite.yml you have enabled all of the modules that you need. I’ve got following in my config:


class_name: FunctionalTester



      - Filesystem

      - Yii2

      - Db

      - REST



            configFile: 'codeception/config/functional.php'


            dsn: 'mysql:host=localhost;dbname=test_db'

            user: 'test_user'

            password: 'test_password'

            dump: 'codeception/_data/dump.sql'

            populate: true

            cleanup: false


            url: 'http://site.test/api'

Next build the environment

codecept build

Now your test may look like this:


/** @var \Codeception\Scenario $scenario */

$I = new FunctionalTester($scenario);

$I->wantTo('ensure that order api works');

/** @var \app\models\User $user */

$user = $I->grabRecord('app\models\User');


$I->sendGET('/orders', ['Order' => ['isNeedExport' => 1]]);

$I->dontSeeResponseContainsJson(['isNeedExport' => 0]);

/** @var \app\models\Order $order */

$order = $I->grabRecord('app\models\Order', ['isNeedExport' => 0]);


$I->seeResponseContainsJson(['isNeedExport' => 0]);



$I->canSeeResponseContainsJson(['isNeedExport' => 1]);




$I->seeResponseContainsJson(['isNeedExport' => 0]);

$I->canSeeInDatabase('order', ['id' => $order->id, 'isNeedExport' => 0]);