Testomatio - Test Management for Codeception
When execution time of your tests is longer than a coffee break, it is a good reason to think about making your tests faster. If you have already tried to run them on SSD drive, and the execution time still upsets you, it might be a good idea to run your tests in parallel.
Codeception does not provide a command like run-parallel
. There is no common solution that can play well for everyone. Here are the questions you will need to answer:
There are two approaches to achieve parallelization. We can use Docker and run each process inside isolated containers, and have those containers executed simultaneously.
Docker works really well for isolating testing environments. By the time of writing this chapter, we didn’t have an awesome tool like it. This chapter demonstrates how to manage parallel execution manually. As you will see we spend too much effort trying to isolate tests which Docker does for free. Today we recommend using Docker for parallel testing.
Please make sure you have docker
installed. Docker experience is required as well.
Run official Codeception image from DockerHub:
docker run codeception/codeception
Running tests from a project, by mounting the current path as a host-volume into the container.
The default working directory in the container is /project
.
docker run -v ${PWD}:/project codeception/codeception run
To prepare application and tests to be executed inside containers you will need to use Docker Compose to run multiple containers and connect them together.
Define all required services in docker-compose.yml
file. Make sure to follow Docker philisophy: 1 service = 1 container. So each process should be defined as its own service. Those services can use official Docker images pulled from DockerHub. Directories with code and tests should be mounted using volume
directive. And exposed ports should be explicitly set using ports
directive.
We prepared a sample config with codeception, web server, database, and selenium with Chrome to be executed together.
Codeception service will execute command codecept run
but only after all services are started. This is defined using depends_on
parameter.
It is easy to add more custom services. For instance to use Redis you just simple add this lines:
By default the image has codecept as its entrypoint, to run the tests simply supply the run command
Run suite
Development bash
And finally to execute testing in parallel you should define how you split your tests and run parallel processes for docker-compose
. Here we split tests by suites, but you can use different groups to split your tests. In section below you will learn how to do that with Robo.
At the end, it is worth specifying that Docker setup can be complicated and please make sure you understand Docker and Docker Compose before proceed. We prepared some links that might help you:
If you want to automate splitting tests by parallel processes, and executing them using PHP script you should use Robo task runner to do that.
Parallel Test Execution consists of 3 steps:
We propose to perform those steps using a task runner. In this guide we will use Robo task runner. It is a modern PHP task runner that is very easy to use. It uses Symfony Process to spawn background and parallel processes. Just what we need for the step 2! What about steps 1 and 3? We have created robo tasks for splitting tests into groups and merging resulting JUnit XML reports.
To conclude, we need:
Execute this command in an empty folder to install Robo and Robo-paracept :
You need to install Codeception after, if codeception is already installed it will not work.
Initializes basic RoboFile in the root of your project
Open RoboFile.php
to edit it
Each public method in robofile can be executed as a command from console. Let’s define commands for 3 steps and include autoload.
If you run robo
, you can see the respective commands:
Codeception can organize tests into groups. Starting from 2.0 it can load information about a group from a files. Sample text file with a list of file names can be treated as a dynamically configured group. Take a look into sample group file:
Tasks from \Codeception\Task\SplitTestsByGroups
will generate non-intersecting group files. You can either split your tests by files or by single tests:
Let’s prepare group files:
Now we have group files. We should update codeception.yml
to load generated group files. In our case we have groups: paracept_1, paracept_2, paracept_3, paracept_4, paracept_5.
Let’s try to execute tests from the second group:
Robo has ParallelExec
task to spawn background processes.
If you are using Docker containers you can launch multiple Codeception containers for different groups:
If you want to run tests locally just use preinstalled taskCodecept
task of Robo to define Codeception commands and put them inside parallelExec
.
In case you don’t use containers you can isolate processes by starting different web servers and databases per each test process.
We can define different databases for different processes. This can be done using Environments. Let’s define 5 new environments in acceptance.suite.yml
:
After the parallelRun
method is defined you can execute tests with
In case of parallelExec
task we recommend to save results as JUnit XML, which can be merged and plugged into Continuous Integration server.
Now, we can execute :
result_paracept.xml
file will be generated. It can be processed and analyzed.
To create one command to rule them all we can define new public method parallelAll
and execute all commands. We will save the result of parallelRun
and use it for our final exit code:
Codeception does not provide tools for parallel test execution. This is a complex task and solutions may vary depending on a project. We use Robo task runner as an external tool to perform all required steps. To prepare our tests to be executed in parallel we use Codeception features of dynamic groups and environments. To do even more we can create Extensions and Group classes to perform dynamic configuration depending on a test process.