Unit and functional testing in php yii2 framework

Published On: 18 November 2016.By .
  • General
  • Quality Engineering

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:

For the global installation you will need to use global directive:

If you’ve never used Composer for global packages before, run composer global status. It should output:

Then add <directory>/vendor/bin to you PATH environment variable. Now we’re able to use codecept from command line globally.

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

Build  the project tests

Final step to run all the tests

To run the single test

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

That’s all for this blog