Tests for Console Commands

(Nima Naraghi7) #1


I have no clue how to create a test for a console command in Yii2 neither unit test nor functional test. Can someone show me an example or a guide please ?


How can I run a command controller in test environment ?


(Alexander Makarov) #2

Check how its done in Yii core tests.

(Jacob Moen) #3

It does not look like Yii is using Codeception at all, which is a shame (considering that it is the de facto testing framework for the framework). :)

(Alexander Makarov) #4

Yii is using it for testing app templates. For unit testing, indeed pure phpunit is used. There will be no difference if you’ll run these tests using Codeception though…

(Cleippi) #5

For anyone looking for the answer, you can test console applications by creating a test configuration in the same way you do for any other Yii2 application (see the advanced template for examples), then creating a test as follows:

class CronCest


    public function sendPromos(FunctionalTester $I)





That’ll run the same thing as if it has been run from the console as

yii cron/send-promos