Suppose you have an application where you need to run your unit tests. The directory structure looks something like this but honestly it doesn’t matter as long as it is somewhere inside your code folder. 

Once you have written you unit tests(In this w have written integration and system tests) as well the only thing remains is to test them from inside the container

https://github.com/deepanshululla/stores_rest_api_flask/tree/docker

Here we have a docker-compose file for building up the images and here we have two containers one for the database and other a flask rest api for crud operations using the database.

version: '2'
services:
web:
build: .
ports:
– "5000:5000"
links:
– db
hostname: web
restart: always
depends_on:
– db
db:
image: mariadb:10.1
environment:
MYSQL_ROOT_PASSWORD: secret-password
MYSQL_DATABASE: db
MYSQL_USER: root
MYSQL_PASSWORD: secret-password
ports:
– "3306:3306"
hostname: db
restart: always

Our testing however note uses sqllite which is an in-memory db.

The way to run your tests from inside the docker container is actually quite simple and is just addition of one line

RUN python -m unittest discover

FROM python:3.6-alpine
ADD . /code
WORKDIR /code
COPY requirements.txt ./
RUN pip install -r requirements.txt
RUN python -m unittest discover
CMD ["python", "app.py"]


0 Comments

What do you think?