Unit Testing

What Are Unit Tests?

A unit test is an automated script designed to verify whether a section of the codebase (a 'unit') behaves as intended. It is a method of 'proving' that the unit of code works using a repeatable, automated methodology, by verifying whether the result matches what is expected.

Unlike a UI test, unit tests do not use the user interface but interact directly with the underlying API.

A unit test must not:

  • interact with a database

  • communicate across a network

  • touch the file system

  • require changes to the environment (such as config files) to run

Codeception uses PHPUnit as the back end for its unit tests, adding some classes and utilities to make writing and using unit tests simpler.

Unit tests allow you to test individual functions, using a variety of input. They are much faster than integration or functional tests, and they help you to be confident that the logic of the function is correct. This is especially useful when changing code within a function, because you can quickly confirm that you have not inadvertently changed its behaviour.

You can run unit tests by changing to the /developers directory and running:

../www-root/core/library/vendor/bin/codecept run unit [filename]:[testfunctionname]

As the above command indicates, you can choose to run only a single test file, or even a single test function within that test file:

../www-root/core/library/vendor/bin/codecept run unit Entrada/HooksTest


../www-root/core/library/vendor/bin/codecept run unit Entrada/HooksTest:testClassMethodHook

Why Unit Test?

Less time performing functional tests

Testing the functionality of software is not only time consuming but costly. Constantly re-testing an application manually can lead to inconsistent results as steps (or tests) are missed despite a seemingly meticulous approach to testing. Switching branches or configuring an application to duplicate the conditions of the test can take time and are often error prone. Each time a change is made to the application a variety of tests must be executed to ensure the stability of the application – this consumes a lot of user resources.

Unit tests – while they take time to develop – soon save money as the tests become available to all members of the engineering department as they ensure consistent test procedures. Unit tests can be as quick as a few milliseconds and do not require knowledge of the application.

Less coupled code

Code that is tightly coupled relies on external resources (objects, classes, etc.) and is very difficult – if not impossible – to test properly. Writing unit tests for your code will allow you to see the value in decoupling code which also leads to a cleaner application.

Executable documentation

It can often be unclear to other developers what a method does or how it behaves when passed certain parameters. By providing an extensive suite of well-named unit tests, each test should be able to verbosely demonstrate the expected output for any provided input. Additionally, it is important for unit tests to prove that the method actually works correctly under both valid AND invalid input conditions.

Protection against regression

When a change is made to the application a new defect can be introduced to the system – this is known as a Regression Defect. When a change is made to the application it is important for testers not only test the new changes, but test the rest of the application to ensure that nothing has been broken by the new change introduced – everything must still behave as expected.

With unit testing, it's possible to rerun your entire suite of tests after every build or even after you change a single line of code.

Last updated