End-to-end tests Isolation with Docker
Incorporating End-to-end (E2E) tests into the development process has several advantages. From a one side, it allows to have a more cleaner code, where any developer can leverage those tests to understand better the working-flow of the targeted code. From the other side, developers feel more confident to refactor their code without breaking the application’s workflow.
But writing good E2E tests takes time and effort. It is worth noting that E2E tests involves a significant amount of setup code. Furthermore, they need to be isolated, so as changes introduced in one tests are not accessible from the next one. However, ensuring E2E test isolation can be almost impossible to implement, especially when methods depend on the other hard-to-control things, such as databases.
The reliance on Docker, and the way its container may be snapshoted/restarted, helped us to guarantee isolation between E2E tests. The general idea is about to dockerize the main parts of our application (e.g., database server, email server, redis server, etc.) into docker containers. These containers are then provisioned with the data required for each E2E test. Afterward, these containers are snapshoted in order to have ready-to-use containers. Then, we are able to restart all the snapshoted containers before each test.
What’s under the hood?
Second step, we introduce a new grunt task to be triggered by Cucumber
Finally we add a hook in the cucumber file so as to restart containers automatically before each E2E test.
To summarize, leveraging Docker container allows us to isolate E2E tests without doing any modifications in their source code. Furthermore, the provisioning process is done only once when bootstrapping the E2E tests.