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.
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 Red phase: Write a failing test.
Let's think about how to build a function that calculates lists of items. What should the interface be? How does the consumer of the code use it? These are the questions we think about first when we "think in tests." Here's one way we might think about it in C++.
Explanation:
- We know we want a function called
CalculateTotal()
, so we'll write a test that uses it. - For now, we know that we want an empty vector as input to return 0, so we can write that test.
- The expectation is that these tests will initially fail, which is an integral part of the Red phase.
Running these tests confirms they fail, creating a clear path for subsequent development.
Now, let's move to the Green phase where we implement the minimal code to pass these tests.
Implement the CalculateTotal
function:
Explanation:
- The
CalculateTotal
function takes a vector ofCartItem
objects. We don't really know the shape of the data yet, and that's okay! - Returning
0.0
is enough to get the test to pass.
By running the test suite again, we should see all tests passing, demonstrating that our function meets the required condition.
Now is the time to think about what kind of data we want to pass to our CalculateTotal
function. We consider that we'd like to pass the name
, price
, and quantity
as properties in the CartItem
struct. The total will be the product of price
* quantity
. Let's do that and see how it feels:
That feels pretty good; the interface seems clear. Let's see if we can get those tests passing.
Now we need to think about how to make these tests pass. What is the minimum necessary to get this to pass?
When we run the tests, we should see that both tests pass! We're Green!
Now we ask ourselves: is there anything we can do to make this code better? One thing I might do is ensure each CartItem
has well-defined properties and leverage a more general approach.
When we run the tests again, we're still green. The code is more expressive now and reflects the properties of CartItem more clearly, so let's do the Red-Green-Refactor loop again!
The existing code works! But it won't be very useful to only use a specific scenario. If we add one more test, we can generalize more.
As expected, this new test will fail and we can move to the Green step.
Let's take a stab at getting the test to pass:
This does the job, and we're Green again. I can't help but feel like we could have written that code a bit better. Now that we have tests that cover everything we want this function to do, let's move to the Refactor step!
When we look at the CalculateTotal
function, it is clear that this is an "aggregate function." It takes a list of items and reduces it to an aggregate value. Using STL algorithms like std::accumulate
can help simplify the function:
The std::accumulate
function efficiently aggregates the total price by multiplying the price and quantity of each CartItem
. This refactoring makes the code concise and expressive while keeping our tests passing.
To run your Google Test suite, you can integrate it with CMake using a basic configuration file.
Create a CMakeLists.txt
file:
Then build and run your tests:
This will build and run your tests, ensuring they are green.
Throughout this lesson, we focused on refining the TDD mindset by emphasizing writing tests prior to coding and following the Red-Green-Refactor cycle. Here's what we covered:
- Red Phase: We started by writing failing tests, like determining that
CalculateTotal
should return 0 for an empty cart and a specific total for a single item. This helped us clearly define our interface and objectives. - Green Phase: We implemented minimal solutions to pass each test condition. For instance, returning a simple calculation for a single
CartItem
or a total for an entire list of items. - Refactor Phase: We improved the code structure and readability, utilizing techniques like STL algorithms for aggregation, ensuring that the function remains expressive and maintainable while tests continue passing.
These steps in the TDD workflow have shown how test-first development can clarify requirements, ensure accuracy, and guide continuous improvement. This foundation prepares you for practice exercises aimed at reinforcing these techniques, highlighting how TDD fosters clarity, reliability, and robustness in software development.
