Welcome back to our course on Test-Driven Development (TDD) in C++ using Google Test. In our previous lesson, we introduced the fundamentals of TDD and the Red-Green-Refactor workflow. Now, we will advance our TDD skills by focusing on generalizing solutions and enhancing the complexity of our testing scenarios.
As a brief reminder, TDD involves a repetitive cycle known as Red-Green-Refactor:
- Red: Write a failing test to clarify the new functionality you aim to implement.
- Green: Develop the smallest amount of code needed to make that test pass.
- Refactor: Clean up the code, enhancing its quality while maintaining its functionality and ensuring all tests remain passing.
In this lesson, we will expand upon the Sum method, demonstrating how to generalize it while following these TDD principles.
Before we dive into coding, let's review our current setup. You are already familiar with the Sum method from Math.hpp, its implementation in Math.cpp, and its corresponding test in MathTest.cpp:
This existing setup serves as a foundation. Now, we'll focus on expanding your understanding by generalizing the approach using TDD principles. Understanding your starting point will help ensure future changes enhance our function without straying too far from the core logic.
To embrace the Red phase, let's introduce a new test case that will fail.
Update MathTest.cpp to include more input scenarios:
By including a new scenario with new inputs, this step is intentionally set to fail to define our target goal clearly.
Upon running this test, you would receive output indicating failure, noting the discrepancy between expected and actual results. This confirms that the new functionality needs addressing.
