Introduction and Overview

In this lesson, we'll deepen our understanding of the Test-Driven Development (TDD) mindset by focusing on the Red-Green-Refactor cycle with a practical example centered on a CalculateTotal function. This example will guide you through the process of thinking in tests, prioritizing test writing, and using TDD to enhance code clarity, reliability, and maintainability.

Using C++ and Google Test, we'll follow these steps:

  • Begin with the Red phase by identifying and writing failing tests for the CalculateTotal function, which will compute the total price of items in a shopping cart.
  • Move to the Green phase to implement the minimal code required to pass each test, ensuring that the CalculateTotal function behaves as expected.
  • Enter the Refactor phase to improve the code structure and readability of CalculateTotal, employing techniques like using STL algorithms while keeping tests green.
  • Utilize Google Test as a testing framework to efficiently integrate the TDD approach within our project.

By working through this example, you'll gain practical experience with TDD principles and develop the CalculateTotal function in a way that showcases how TDD fosters code quality and robustness.

Example: `CalculateTotal` Function (Red Phase)

Let's begin by writing tests for a function named CalculateTotal, designed to compute the total price of items in a shopping cart. This is where you engage with the : Write a failing test.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal