Unit and functional testing in php yii2 framework
- General
- Quality Engineering
Unit and functional testing in php yii2 framework
There are plenty of ways to test your application. The most popular paradigm is Unit Testing. As for web applications, testing the controller, or model in isolation doesn’t prove your application is working. To test the behavior of your application as a whole, you should write functional or acceptance tests.
Yii 2 has officially maintained integration with Codeception
testing framework that allows you to create the following test types:
- Unit testing – verifies that a single unit of code is working as expected;
- Functional testing – verifies scenarios from a user’s perspective via browser emulation;
- Acceptance testing – verifies scenarios from a user’s perspective in a browser.
Yii provides ready to use test sets for all three test types in both yii2-basic
and yii2-advanced
project templates.
In order to run tests you need to install Codeception. You can install it either locally – for particular project only, or globally – for your development machine.
For the local installation use following commands:
1 2 3 |
composer require "codeception/codeception=2.1.*" composer require "codeception/specify=*" composer require "codeception/verify=*" |
For the global installation you will need to use global
directive:
1 2 3 |
composer global require "codeception/codeception=2.1.*" composer global require "codeception/specify=*" composer global require "codeception/verify=*" |
If you’ve never used Composer for global packages before, run composer global status
. It should output:
1 |
Changed current directory to <directory> |
Then add <directory>/vendor/bin
to you PATH
environment variable. Now we’re able to use codecept
from command line globally.
1 |
export PATH=$PATH:<directory>/vendor/bin |
Now you will see all required folders will be copied in your project tests folder. Yii2 gives you basic tests functions for Home page, Login Page and Contact us page. You can simply build and run.
Initialize the project
1 |
codecept bootstrap |
Build the project tests
1 |
codecept build |
Final step to run all the tests
1 |
codecept run |
To run the single test
1 |
codecept run functional SigninCept.php |
Bootstrap File
Each suite has its own bootstrap file. It’s located in the suite directory and is named _bootstrap.php
. It will be executed before test suite. There is also a global bootstrap file located in the tests
directory. It can be used to include additional files.
Configuration
Codeception has a global configuration in codeception.yml
and a config for each suite. We also support .dist
configuration files. If you have several developers in a project, put shared settings into codeception.dist.yml
and personal settings into codeception.yml
. The same goes for suite configs. For example, the unit.suite.yml
will be merged with unit.suite.dist.yml
.
Actors
One of the main concepts of Codeception is representation of tests as actions of a person. We have a UnitTester, who executes functions and tests the code. We also have a FunctionalTester, a qualified tester, who tests the application as a whole, with knowledge of its internals. And an AcceptanceTester, a user that works with our application through an interface that we provide.
Actor classes are not written but generated from suite configuration. Methods of actor classes are generally taken from Codeception Modules. Each module provides predefined actions for different testing purposes, and they can be combined to fit the testing environment. Codeception tries to solve 90% of possible testing issues in its modules, so you don’t have reinvent the wheel. We think that you can spend more time on writing tests and less on writing support code to make those tests run. By default AcceptanceTester relies on PhpBrowser module, which is set in tests/acceptance.suite.yml
configuration file:
For more details you can visit http://codeception.com/docs/02-GettingStarted
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s