Test Driven Development in Software

Danin Sudjono
PDB+R
Published in
3 min readApr 17, 2019

Hello, lovely readers of our Medium. Today I’ll be talking about something called Test Driven Development or TDD for short.

Not this kind of test

What’s TDD?

Test Driven Development is one of many software development methods. Test Driven Development combines two other development methods, Test First Development (TFD) and refactoring. What TDD tries to achieve is well-developed code by creating the tests first. These tests are used to check the functionality of the code we are trying to make. After the tests are made, initial code is worked on, and refactored as needed to pass all the tests created at the start.

Another goal of Test Driven Development is to reach 100% code coverage. Code coverage is the result of testing lines of code, which lines are covered in the test, and which ones aren’t.

In TDD, there is an approach using three phases, called “Red, Green, Refactor”. These three phases help developers divide their work into three parts:

Red: Making the tests
Green: Passing the tests
Refactor: Improving code

Image result for test driven dilbert

Why TDD?

There are a few points as to why TDD is useful, especially in agile programming:

  • Because agile programming requires product delivery every sprint, we can easily test if the code has fulfilled the requirement for said product delivery
  • Instant feedback on what we need to do to fix the code
  • Works as documentation. Easily discover bugs.
  • Supports adaptability of code and software requirements

What We’ve Done

On our Django Project, we have implemented several test cases for the apps we are working on. Here is an example of our test code:

The test above checks if the URL exists or not by checking if the status code of the response is 200 or not.

After creating the test above, we implement the URL that we want to test:

Afterwards we can run the test by running the following code:

and we can see our coverage in the coverage report, using coverage report -m

This is only a small part of the whole code, and the coverage for this particular test is complete. There will be cases where we need to refactor code in order to reach 100% code coverage.

That’s it for this article. Hope you learned something. Until next time.

--

--