Skip main navigation

Test-Driven Development (TDD)

Test-driven development, or TDD, is a development methodology that lets the tests drive the production of code.

In the last step, we learned about unit tests and characteristics of proven unit tests. In this step, we cover test-driven development (TDD).

Test-driven development, or TDD, is a development methodology that lets the tests drive the production of code. This ensures that the tests reflect what the code ought to do and that all methods in the public class interface are tested

The essential points are:

  • Tests, based on specifications, are written before any code.
  • The aim is to pass the tests; anything else is superfluous.
  • Lines of code are written only if they help a class pass a test.
  • After all the tests are passed, the code is complete.

The steps are:

  1. Quickly add a test.
  2. Run all tests and see the new one fail.
  3. Make a little change.
  4. Run all tests and see them succeed.
  5. Refactor to remove duplication.

Other helpful unit-testing processes

  • Stubs. Stubs provide panned answers to calls made during the tests, usually not responding at all to anything outside what’s programmed in for the test.
  • Mocks. These are objects pre-programmed with expectations that form a specification of the calls that they are expected to receive.
  • Fakes. These are objects that actually have working implementations but usually take some shortcut that makes them unsuitable for production (in-memory database, for example).

Remember to engage in peer discussions or share experiences in the comment section below. In the following step, we will explore integration tests.

This article is from the free online

Microsoft Future Ready: Fundamentals of DevOps and Azure Pipeline

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education