Table of Contents
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:
- Solve complex issues in human brain
- Find out what is the reason for issue
- Scale up the complexity of codebase without knowing the expected errors and bugs
- 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:
- First the developer writes a failing test case that defines a desired improvement or new function
- then produces code to pass the test
- 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: #
- Helps documenting your code while it grows and scales up
- Improves codebase design
- Catches future errors
- Long-term time savings
- Reduces technical debt and hence risk
- 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
./manage.py test test command all unit testing jobs can be done manually. But we can make it automated with using
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:
- Build disk images of software that can be deployed anywhere, on any platform
- Distribute application's operating system with a team
- Run local code on laptop in the same environment as on production server
- 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 #
$ django-admin.py startproject myproject $ cd myproject $ ./manage.py 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
First test #
Before we start testing we must add first test case to
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)
Conclusion for this part #
In this part we introduced with Django TDD, next part will include Docker implementation.