Lesson 1: A Step by Step Tutorial on How to Use Test Driven Development When Developing Applications Using Django 2.2 and Python 3.7.

Lesson 1: A Step by Step Tutorial on How to Use Test Driven Development When Developing Applications Using Django 2.2 and Python 3.7.

Hello and welcome to this Python/Django tutorial series, my name is Henry Mbugua and I will be taking you through the various aspects and new answers of how to build better software using Test Driven Development. Developing an application is very difficult, even for a medium-size project which have many moving parts. Software developers are not only expected to get those parts moving but keep them moving as the application changes over time. In this tutorial series, we are going to learn the process of developing a Django app by writing failing test cases first, then we write the application code to make those test cases pass.

What is Test-Driven Development?

“All code is guilty until proven innocent.” – Andrea Koutifaris

In order for us to understand what is Test-Driven Development (TDD), we are going to break the definition into four parts. In fact, TDD is the practice of:

  • Part 1 – writing deliberately failing test cases.
  • Part 2 – writing your application code to make the test cases pass.
  • Part 3 – refactoring to optimize your code while the test cases continue to pass.
  • Part 4 – repeating the process until your project is complete.

Human Example of Test Case

Let’s assume you are a teacher, you have given your student assignment to multiply 6 by 10. As a teacher, you would be expecting the student to give an output of 60 and confirm that the answer is correct. This is the same principle that is applied in TDD, how can we teach our application to do confirmation for itself?

Importance Test-Driven Development

From the outset, TDD seems like a lot of work. The following are a few reasons why it’s worth it:

  1. Writing testable code – Code that is easily tested is a better code. Writing the test before you write code will force a developer to write code that can easily be tested.
  2. You will look like a pro – when you release your code out into the world, either as a user-facing application or installable package for developers, you are making a promise that the documentation of your code is accurate and your code does what is supposed to do.
  3. Work better with a team – An important part of working in a development team is explaining the code you write to your fellow developers. There is no better way to explain your code than a walk through your test cases. Better yet, write test cases as a team to foster collaboration.
  4. Clarify your thinking – Computer applications are abstract models of real-world systems that solve problems for people around the world. Abstracting solutions to human problems in computer code takes serious thought and care. By clearly defining the functionality of your application with a test case before you try to develop, you force yourself to write code with the end goal in mind.
  5. Building only what is required – Codifying your project requirements as test cases and only writing enough code that makes the test cases pass will ensure that you have fulfilled all the user stories and guard against any scope creep.

Tools that we will be using

Writing test cases in our application is a pillar of professional software development. The following are key practices to writing great code:

a) Version control – Version control allows developers to check code changes into a repository at regular intervals and rollback to any of those changes later. We will be using Git throughout this tutorial series. Make sure you have a Github account.

Travis CI
GitHub

b) Continuous Integration – All the glorious test will be pretty useless if no one is running them. A Continuous Integration (CI) server, for our purpose, can pull our project from version control, build it up, run our test cases and alert us if there are errors or failure occurs. We will be using the Travis Continuous Integration server through this tutorial. Make sure you have an account with Travis CI.

Travis CI
Travis CI

Tasks

Before our next tutorial, make sure you have the following:

With that, we conclude this lesson. In lesson 2, we will start a django project from scratch using rigorous TDD methodology, learning some of the testing tools available in Django and Python along the way.

Facebook Comments

Close Menu