Welcome back to our course on Test-Driven Development (TDD) using Swift and XCTest
. 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
function, 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
function in MathFunctions.swift
and its corresponding test in MathFunctionsTests.swift
:
MathFunctions.swift
Swift1func sum(_ a: Int, _ b: Int) -> Int { 2 return 5 3}
MathFunctionsTests.swift
Swift1import XCTest
2
3class MathFunctionsTests: XCTestCase {
4
5 func testSum() {
6 let result = sum(2, 3)
7 XCTAssertEqual(result, 5)
8 }
9}
This existing setup serves as a foundation. Now, we'll focus on expanding your understanding by generalizing the approach using TDD principles. Understanding where you've come from 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 MathFunctionsTests.swift
to include more input scenarios:
Swift1import XCTest
2
3class MathFunctionsTests: XCTestCase {
4
5 func testSum() {
6 let result = sum(2, 3)
7 XCTAssertEqual(result, 5)
8 }
9
10 func testSumTwoMore() {
11 let result = sum(5, 6)
12 XCTAssertEqual(result, 11)
13 }
14}
By including a new scenario with new inputs, this step is intentionally set to fail to define our target goal clearly.
Running this test with XCTest
will demonstrate a failure, indicating the need for new functionality.
Now, let's transition to the Green phase, where our goal is to ensure all tests pass, including our new one. We can update our sum
function to use the generic response because it is the minimal solution.
MathFunctions.swift
Swift1func sum(_ a: Int, _ b: Int) -> Int {
2 return a + b
3}
Running the tests again should now output a successful result. This illustrates how effective writing minimal code to pass tests can be.
Finally, let's advance into the Refactor stage. The sum
function is very simple, so there's nothing we can make better with the implementation. We do have a lot of duplication in our tests, however.
Let's introduce a helper function to reduce duplication. This will make it easy to add a lot of test cases without duplicating too much test code.
MathFunctionsTests.swift
Swift1import XCTest
2
3class MathFunctionsTests: XCTestCase {
4
5 func testSum() {
6 verifySum(a: 2, b: 3, expected: 5)
7 verifySum(a: 5, b: 6, expected: 11)
8 }
9
10 private func verifySum(a: Int, b: Int, expected: Int) {
11 let result = sum(a, b)
12 XCTAssertEqual(result, expected)
13 }
14}
For now, there's no functional change — but strategically refactoring in this way offers benefits, maintaining clarity as complexity escalates over iterations.
In this lesson, we explored advancing our TDD approach by generalizing solutions and scaling scenarios:
- Red: Write a failing test to define new goals.
- Green: Confirm new goals via minimal passing code.
- Refactor: Support future needs by formalizing and improving code.
These principles remain central to creating robust, adaptable solutions in any software development process. Continue practicing these steps through additional tests and scenarios in your exercises. You're on the path to mastering evolving solution implementations and designing more comprehensive, generalized code with TDD. Keep applying these principles and practicing to further cement your understanding.
