Skip to content

Django Test Driven Development with Docker #

Find similar titles

11회 업데이트 됨.

  • 최초 작성자
  • 최근 업데이트

Structured data


Introduction #

Testing the code we write is always important. It helps us avoiding future bugs and improves code quality. When codebase gets changed often, testing helps to coverage untested units and also makes easier to make documentation.

Testing is important because it is difficult to:

  1. Solve complex issues in human brain
  2. Find out what is the reason for issue
  3. Scale up the complexity of codebase without knowing the expected errors and bugs
  4. Make sure for each software unit can meet the requirement and show the expected performance

Test Driven Development(TDD) helps to solve above issues. All software can have bugs. So, there is no way to guarantee that TDD makes your code error-free, but it helps to minimize the risk rate.

Why TDD? #

Test Driven Development(TDD) is a software development process that relies on the repetition of a very short development cycle:

  1. First the developer writes a failing test case that defines a desired improvement or new function
  2. then produces code to pass the test
  3. and finally refactors the new code to acceptable standards


Nowadays TDD became industry standard. Modern developer things that any piece of software code must be tested.

Advantages of TDD: #

  1. Helps documenting your code while it grows and scales up
  2. Improves codebase design
  3. Catches future errors
  4. Long-term time savings
  5. Reduces technical debt and hence risk
  6. Avoid manual one-off tests. Eventually, you will add and re-add test data to test manually

These are advantages gathered from various developers who practice TDD. Each of them merit a detailed explanation. But to summarise, TDD brings with it a lot of benefits of testing by making it a mandatory part of your development cycle. You might think that you will add tests later, but sometimes you never get around to doing it.

Code written by passing simple, focused test cases tend to be more modular and hence better designed. It is a pleasant side effect of the process but you will certainly notice it.

TDDs can be organized as manual and automated testing. With using Django's traditional ./ test test command all unit testing jobs can be done manually. But we can make it automated with using [[Vim]], [[Tmux]] and [[Docker]].

Automated testing is an extremely useful bug-killing tool for the modern Web developer. You can use a collection of tests – a test suite – to solve, or avoid, a number of problems:

  • When you’re writing new code, you can use tests to validate your code works as expected.
  • When you’re refactoring or modifying old code, you can use tests to ensure your changes haven’t affected your application’s behavior unexpectedly.

What is Docker and why should I use it? #


Docker is a manager that automates deployment of applications inside the software containers. Docker implements a high-level API to provide lightweight containers that run processes in isolation. Also, Docker is like a Git. It is a version control system for software operating system.

We should use Docker because it helps us to:

  1. Build disk images of software that can be deployed anywhere, on any platform
  2. Distribute application's operating system with a team
  3. Run local code on laptop in the same environment as on production server
  4. Catch production errors on laptop without deploying the code to server

Why Docker for TDD? #

TDD is itself can be hard for many cases. For example, testing HTTPS handling in Django, hidden performance failure testing, unittest response from WSGI application, etc. Docker is useful for TDD for testing above-mentioned test cases. In this article, we are going to propose free and portable Django TDD environment with Docker.

Traditional Django unit testing #

In Django unit testing, the traditional way to test your Django code base is similar to following steps. Let's write our simple test.

Create the Django project #

$ startproject myproject
$ cd myproject
$ ./ startapp core

After we configure DATABASES settings with default simple file-based sqlite3 database and add core app in INSTALLED_APPS, we syncronize the database with ./ syncdb

First test #

Before we start testing we must add first test case to core/

from django.test import TestCase
from core.models import Post

class BlogPostTestCase(TestCase):
    def test_post(self):
        test_title = 'test title'
        post = Post.objects.create(title=test_title)
        self.assertEquals(post.title, test_title)

Docker environment #

Automated Django TDD #

Suggested Pages #