Codefresh is a Docker-native CI/CD platform.

Instantly build , test and deploy Docker images.

Search results for "{{ search.query }}"

No results found for "{{search.query}}". 
View All Results

Unit Test with Database

Sometimes running unit tests requires some additional services like a Database (Mongo, MySql, Postgres etc), or third-party components like Redis, Memcache, etc.

Codefresh provides you an easy way to do it by using the Codefresh pipeline and docker-compose.

To run Unit Tests with a DB or other services, you can create a composition and run your unit tests as part of it.

Example repository

Fork this repository in Github to continue

How to do it using option "Attach to Composition"

  1. Navigate to added example repository, select relevant pipeline and put in your test script in Unit Tests Section. For this example, your unit test script will be something like this. Just copy and paste this script to section of Unit Test Script

wait_for_db() {
  nslookup db
  if ! nc -z db 3306; then
    echo "Waiting for db..."
    sleep 5


export MYSQL_USER=my_user
export MYSQL_HOST=db
export MYSQL_PASSWORD=admin
export MYSQL_DATABASE=nodejs

npm test
  1. Go to composition module and create a new composition just press ("+"), the wizard will propose you a few options. The most useful options is to create a new one or add docker-compose.yml from your repository. For this example just copy paste the following docker-compose.yml
version: '2'
    image: 'mysql:latest'
      - 3306
      - MYSQL_USER=my_user
      - MYSQL_PASSWORD=admin
      - MYSQL_DATABASE=nodejs
  1. Put in the relevant composition component, for example, mysql and save with relevant name.
  2. Go to back to pipelines of this repository
  3. Select the Run tests with composition option and choose the created composition you wish to run your image in while running the Unit Tests.
  1. Save and Build this pipeline

See unit test examples with other databases

Unit Test with Database