Integration Tests with MySQL
Launching a MySQL service container
In this example we will see a NodeJS project that is using MySQL for data storage. For the integration test phase we will launch an instance of MySQL in order to run a simple integration test.
The Integration tests are looking for a MySQL connection at
The example NodeJS project
You can see the example project at https://github.com/codefreshdemo/cf-example-unit-tests-with-composition. The repository contains the NodeJS source code and the simple integration test.
You can play with it locally by using Docker compose to launch both the application and the MySQL Database.
Create a pipeline with MySQL integration tests
Here is the whole pipeline:
This pipeline does the following:
- Clones the source code with a Git clone step.
- Builds a Docker image with the integration test.
- Runs the tests while launching a service container for an active MySQL instance passing the required environment variables (that match what the test is expecting).
Notice that both the DB as well as the tests share a set of variables (
MYSQL_USER etc.) and thus we use YAML anchors to avoid duplication.
Notice that we also use the
readiness property in the testing phase so that we can verify MySQL is ready and listening, before running the tests.