Run unit tests
Running unit tests in Codefresh pipelines
In this example, we will see both ways using two different applications in a single pipeline.
In the first case, we run unit tests before creating the application docker image. In the second case, we run the unit tests inside the application Docker image.
Example Python/Go project
You can see the example project at https://github.com/codefreshdemo/cf-example-unit-test. The repository contains two applications (Python and Go) with their respective unit tests.
You can play with it locally by using Docker commands to package the applications.
Create a pipeline with unit tests
Here is the whole pipeline:
This pipeline does the following:
- Clones the source code through a Git clone step.
- Runs unit test for the GO application using the Dockerhub image
- Builds the Docker image for the Go application through a build step.
- Builds the Docker image for the Python application.
- Runs unit tests for the Python application using as runtime context the application image that was just created.
In the second case, the tests run in the context of
build_before_my_tests which is the name of the step that creates the Docker image for Python. Read more about context variables.
We generally recommend the first approach, so that your production Docker image does not contain any unit testing libraries or frameworks, but there is no right or wrong choice regarding the way you run unit tests.