TravisCI & Flake8 with Docker for Test-Driven Development (TDD)

fc53b7_aff84f7057704c96ae8828026bc59d93_mv2.webp

Django, as we know, is all-encompassing, with a DB with an ORM , Authentication system, Django Rest Framework , built-in tests, and a web server gateway interface, its a power-house of a framework for web development.

Docker as we know changed the entire software development paradigm by containerizing what these frameworks could do and automation for CI/CD from Travis-CI makes it a great combination.

In this article, let's see how to get started with a Django project using Travis-CI, Docker, and to check for lint errors, we'll use Flake8. I Will also use PostgreSQL as a database for our project due to its popularity with this combination. I intend to only show how to set up the project. To know more on how to write tests, build this entire project, you may choose to follow this on GitHub here

It's done in the order of the following steps

Part 1: Set up the project

Create a new repository on Github

Add Dockerfile

Configure Docker Compose

Create a Django project

Part 2: Set up automation

   5. Enable Travis-CI 

   6. Create Travis-CI configuration file

       7. Start writing tests
  1. Open Github and create a new repository, initiate it with a Readme.md, and an MIT license perhaps.

  2. Create a new file called 'Dockerfile' and write the code below

FROM python:3.8-alpine
MAINTAINER Madhusudhan Anand

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache postgresql-client
RUN apk add --update --no-cache --virtual .tmp-build-deps \
 gcc libc-dev linux-headers postgresql-dev

RUN pip install -r /requirements.txt
RUN apk del .tmp-build-deps

RUN mkdir /app
WORKDIR /app
COPY ./app /app

RUN adduser -D user
USER user
  1. Then create another file docker-compose.yml
version: "3.3"

services:
 app:
 build:
 context: .
 ports:
            - "8000:8000"
 volumes:
            - ./app:/app
 command: >
            sh -c "python manage.py wait_for_db &&
                   python manage.py migrate &&
                   python manage.py runserver 0.0.0.0:8000"
 environment: 
            - DB_HOST=db
            - DB_NAME=app
            - DB_USER=postgres
            - DB_PASS=supersecretpassword
 depends_on: 
            - db
 db: 
 image: postgres:10-alpine
 environment:
            - POSTGRES_DB=app
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=supersecretpassword

Then create a file where we will include all our file requirements for docker to build from. name it requirements.txt

Django>=2.1.3,<3.0.6
djangorestframework>=3.9.0,<3.11.0
psycopg2>=2.7.5,<2.9.0

flake8>=3.6.0,<3.8.0
  1. Open terminal(Mac or Linux)/Powershell(windows), (assuming you have already cloned your git repo) CD into your project, and type the command

django-admin startproject mysite This will create a new django project.

  1. Now go to Travis-ci.org and sign up with your GitHub login and activate the repository that you created

6 & 7. Then create a new file, with file name .travis.yml, and write the code below

language: python
python:
    - 3.8

services: 
    - docker

before_script: pip install docker-compose

script:
    - docker-compose run app sh -c "python manage.py test && flake8"
  1. That's it, now you can start running your tests first. I have worked a bit on this and I am halfway through the project. You can follow and look at all the test files to know how to write tests and also in addition to this, you may review the documentation.

Happy Coding!