TravisCI & Flake8 with Docker for Test-Driven Development (TDD)
Embarking on a Journey: Unveiling My Passions and Pursuits Greetings from Bangalore, India! My name is Madhusudhan Anand, and life has been a beautiful ride of experiences and challenges. Growing up, my family's nomadic nature led us to traverse various cities in Karnataka, immersing me in the rich tapestry of diverse cultures. These encounters have left an indelible mark on my journey, shaping my passions across four distinctive realms: product development, teaching, problem-solving, and writing. As the co-founder of Ambee, a vibrant climate tech startup, my forte is transforming promising ideas into tangible, revenue-generating products. I channel my creative energy, technical expertise, and entrepreneurial spirit with every project to make a meaningful impact. Teaching has become more than just a hobby—it has become a way for me to ignite a spark of knowledge and inspiration in others. Over the years, I've had the privilege of mentoring and training over 2000 programmers worldwide. Sharing my insights and empowering aspiring talents in the world of data science and programming has been a profitable endeavor. Problem-solving is the fuel that drives my passion. With an optimistic and multidimensional perspective, I approach every challenge as an opportunity for growth. From my roots in data science and remote sensing to exploring the realms of climate change, IoT, and AI, I've harnessed my problem-solving prowess to create innovative products at Ambee. Writing has always been my sanctuary—an avenue to channel my thoughts, emotions, and ideas. I am captivated by the power of the written word to inspire, educate, and connect. Through my blog, I promise to deliver authentic, informative, and infused content with my personal touch. I'll share insights from my journey, staying true to my values and unwavering commitment to honesty. As I embark on this blogging adventure, I dedicate this platform to my late father, a constant source of inspiration and strength. His memory will forever reside in my heart, guiding me to be true to myself and positively impact the world. Join me on this exhilarating journey of exploration, learning, and growth. Let's delve into the fascinating realms of technology, data science, and personal reflections. Welcome to my world!

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
Open Github and create a new repository, initiate it with a Readme.md, and an MIT license perhaps.
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
- 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
- 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.
- 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"
- 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!





