Yii2 Unit and Functional testing

Kevin Kariuki
2 min readOct 9, 2017

We all know deep down how essential it is to write unit test. In my many years in software engineering I have never really gotten to include automated testing to my projects.Yes, yes, I know you are wondering how is that even possible. Well I have a list of excuses the best of them all is; This project is behind schedule and you know writing unit and functional test will take up much of my time of which I really don’t have.

But the reality starts to bite once we have a live and somewhat legacy system. A small change in the code and the only hope is that God protects you from all bugs formed against you on a friday night.

In a quest to improve the quality of code I build I have challenged myself to learn how to write unit and functional test and share with you.

So our mission will be to understand how Yii2 uses codeception for both its unit, functional and acceptance tests.

Getting started with Yii2 and Codeception

Yii2 Guide is very clear on the setup for automated test, ensure you have installed codeception on your machine using the following:

composer global require “codeception/codeception=2.1.*”
composer global require “codeception/specify=*”
composer global require “codeception/verify=*”

Yii2 has 3 maintained integration with codeception:

  1. Unit test: checks that a single code is working
  2. Functional test: verifies that a scenario from a user perspective is working e.g. user login
  3. Acceptance test: more similar to functional testing altho acceptance testing verifies functionality as described in a user journey through a browser.

By default yii2 basic and advance template have already sample unit, functional and acceptance test. To test that you have configure everything before proceeding you can run on your project root

Yii2 unit, functional and acceptance test

The above command will run all the functional, unit and acceptance test defined in the system. On a clean installation Yii2 has defined unit test for login, user authentication and the contact us page. On success installation you should see passed and failed test. One key importance is to note where all these tests are defined.

Yii Skeleton basic template

Yii2 Unit Tests

***Will Be updated***

--

--