Testomatio - Test Management for Codeception
Codeception uses PHPUnit as a backend for running its tests. Thus, any PHPUnit test can be added to a Codeception test suite and then executed. If you ever wrote a PHPUnit test then do it just as you did before. Codeception adds some nice helpers to simplify common tasks.
Create a test using
generate:test command with a suite and test names as parameters:
It creates a new
ExampleTest file located in the
As always, you can run the newly created test with this command:
Or simply run the whole set of unit tests with:
A test created by the
generate:test command will look like this:
Inside a class:
testprefix are tests
_beforemethod is executed before each test (like
_aftermethod is executed after each test (like
Unit tests are focused around a single component of an application. All external dependencies for components should be replaced with test doubles.
A typical unit test may look like this:
There are pretty many assertions you can use inside tests. The most common are:
Assertion methods come from PHPUnit. See the complete reference at phpunit.de.
Codeception provides Codeception\Stub library for building mocks and stubs for tests. Under the hood it used PHPUnit’s mock builder but with much simplified API.
Alternatively, Mockery can be used inside Codeception.
Stubs can be created with a static methods of
Inside unit tests (
Codeception\Test\Unit) it is recommended to use alternative API:
Stubs can also be created using static methods from
See a reference for static Stub API
To declare expectations for mocks use
Unlike unit tests integration tests doesn’t require the code to be executed in isolation. That allows us to use database and other components inside a tests. To improve the testing experience modules can be used as in functional testing.
As in scenario-driven functional or acceptance tests you can access Actor class methods.
If you write integration tests, it may be useful to include the
Db module for database testing.
To access UnitTester methods you can use the
UnitTester property in a test.
Let’s see how you can do some database testing:
To enable the database functionality in unit tests, make sure the
Db module is included
unit.suite.yml configuration file.
The database will be cleaned and populated after each test, the same way it happens for acceptance and functional tests.
If that’s not your required behavior, change the settings of the
Db module for the current suite. See Db Module
You should probably not access your database directly if your project already uses ORM for database interactions.
Why not use ORM directly inside your tests? Let’s try to write a test using Laravel’s ORM Eloquent.
For this we need to configure the Laravel5 module. We won’t need its web interaction methods like
so let’s enable only the ORM part of it:
We included the Laravel5 module the same way we did for functional testing. Let’s see how we can use it for integration tests:
A very similar approach can be used for all frameworks that have an ORM implementing the ActiveRecord pattern.
In Yii2 and Phalcon, the methods
dontSeeRecord work in the same way.
They also should be included by specifying
part: ORM in order to not use the functional testing actions.
If you are using Symfony with Doctrine, you don’t need to enable Symfony itself but just Doctrine2:
In this case you can use the methods from the Doctrine2 module, while Doctrine itself uses the Symfony module to establish connections to the database. In this case a test might look like:
In both examples you should not be worried about the data persistence between tests. The Doctrine2 and Laravel5 modules will clean up the created data at the end of a test. This is done by wrapping each test in a transaction and rolling it back afterwards.
Codeception allows you to access the properties and methods of all modules defined for this suite. Unlike using the UnitTester class for this purpose, using a module directly grants you access to all public properties of that module.
We have already demonstrated this in a previous example where we accessed the Entity Manager from a Doctrine2 module:
If you use the
Symfony module, here is how you can access the Symfony container:
The same can be done for all public properties of an enabled module. Accessible properties are listed in the module reference.
Cest format can also be used for integration testing.
In some cases it makes tests cleaner as it simplifies module access by using common
This format can be recommended for testing domain and database interactions.
In Cest format you don’t have native support for test doubles so it’s recommended
to include a trait
\Codeception\Test\Feature\Stub to enable mocks inside a test.
Alternatively, install and enable Mockery module.
When writing tests you should prepare them for constant changes in your application. Tests should be easy to read and maintain. If a specification of your application is changed, your tests should be updated as well. If you don’t have a convention inside your team for documenting tests, you will have issues figuring out what tests will be affected by the introduction of a new feature.
That’s why it’s pretty important not just to cover your application with unit tests, but make unit tests self-explanatory. We do this for scenario-driven acceptance and functional tests, and we should do this for unit and integration tests as well.
For this case we have a stand-alone project Specify (which is included in the phar package) for writing specifications inside unit tests:
specify codeblocks, you can describe any piece of a test.
This makes tests much cleaner and comprehensible for everyone in your team.
specify blocks is isolated. In the example above, any changes to
will not be reflected in other code blocks as it is marked with
Also, you may add Codeception\Verify for BDD-style assertions.
This tiny library adds more readable assertions, which is quite nice, if you are always confused
about which argument in
assert calls is expected and which one is actual:
The more complicated your domain is the more explicit your tests should be. With DomainAssert library you can easily create custom assertion methods for unit and integration tests.
It allows to reuse business rules inside assertion methods:
With custom assertion methods you can improve readability of your tests and keep them focused around the specification.
AspectMock is an advanced mocking framework which allows you to replace any methods of any class in a test. Static methods, class methods, date and time functions can be easily replaced with AspectMock. For instance, you can test singletons!
By default Codeception uses the
E_ALL & ~E_STRICT & ~E_DEPRECATED error reporting level.
In unit tests you might want to change this level depending on your framework’s error policy.
The error reporting level can be set in the suite configuration file:
error_level can also be set globally in
codeception.yml file. In order to do that, you need to specify
error_level as a part of
settings. For more information, see Global Configuration. Note that suite specific
error_level value will override global value.
PHPUnit tests are first-class citizens in test suites. Whenever you need to write and execute unit tests, you don’t need to install PHPUnit separately, but use Codeception directly to execute them. Some nice features can be added to common unit tests by integrating Codeception modules. For most unit and integration testing, PHPUnit tests are enough. They run fast, and are easy to maintain.