What is Test-driven Development?

Get introduced to the test-driven development approach, and learn why we should write automated tests for our application.

Welcome to the test-driven development approach in Django. This course is unique since it’s project based. By the end of this course, we’ll have a fully working e-library application with some custom tests. Writing tests for our Django project is important, especially for serious software engineering projects.

Why write tests?

Before we discuss what test-driven development entails, it’s important to know why we write tests for an application or software. We’ll face major issues down the road if we expect our Django application to act a certain way while making changes to the codebase without any tests.

Tests can be manual or automated. We can test each functionality manually in a scenario where an application is small. An example of this could be a personal website or blog with very minute functionality.

Manual tests involve the programmers proactively testing each part of the application. We do this to make sure everything works as expected as we make changes to the codebase. However, this quickly becomes difficult to manage as the project grows.

Therefore, we have the concept of automated tests, where we write code that tries to simulate an actual manual test. However, instead of us testing it proactively, the machine (computer) tests it for us.

This course focuses on writing automated tests.

Customer-driven applications that numerous clients or users depend on can’t afford to have serious bugs because this can lead to several vulnerabilities. Bugs arise due to programming errors. Programming errors are a natural part of writing code. Writing tests can mitigate this by catching such bugs before the code is made live.

Writing tests is very important. Writing numerous tests inevitably saves us time in the long run.

Approaches involved in writing automated tests

There are two approaches to writing automated tests for our application. The first approach is to write the code first, and then write the tests. This approach is quite popular since developers prefer to first build out the functionality or feature before writing the tests to make sure the feature works.

However, we have another approach where we write the tests before we write the code. This is known as test-driven development (TDD). This means that the development of the application is driven by the tests we write. Therefore, we can’t add functionality or features without first writing the test.

Note: Both approaches work. The important thing is that we should write tests since Django projects quickly start to grow. However, for this course, our main focus is on the second approach.

The advent of test-driven development

The term test-driven development was coined by Kent Beck in 2002 in his book Test-Driven Development by Example. Since then, this term has gained popularity in the web development space. Several large corporations use this technique due to its numerous advantages.

However, implementing TDD doesn’t come naturally. The developer has to make a conscious effort. This is because most developers prefer to first code the functionality of the application before testing it. So, we can say that TDD is like a discipline that has to be cultivated.