Run Unit Testing amp Code Coverage Jobs using Services
This guide explains how to configure Docker-based services in a GitLab CI/CD pipeline for unit testing and code coverage.
In this guide, you’ll learn how to configure Docker-based services within a GitLab CI/CD pipeline. Services are Docker containers that provide network-accessible dependencies (e.g., databases, caches) so your jobs can execute tests against them.
GitLab CI services let your job containers connect to supporting containers on the same network. Common use cases include databases (PostgreSQL, MongoDB), message queues (RabbitMQ), or custom APIs.
CI/CD Jobs: Unit Testing & Code Coverage with MongoDB Service
The example below shows two jobs—unit_testing and code_coverage—both using a MongoDB service from siddharth67/mongo-db:non-prod. We set an alias mongo and always pull the image on self-managed runners.
Running with gitlab-runner 16.6.0...Using Docker executor with image node:17-alpine3.14 ...Starting service siddharth67/mongo-db:non-prod ...Pulling docker image siddharth67/mongo-db:non-prod ...Waiting for services to be up and running (timeout 30 seconds)...Pulling docker image node:17-alpine3.14 ...Preparing environmentRunning on runner-xyz...Getting source from Git repositoryRestoring cacheExecuting "step_script" stage of the job script$ npm test ✓ should connect to MongoDB at mongo:27017...Saving cache for successful jobUploading artifacts for successful jobJob succeeded