TDD

The interviewer asks

“What is TDD? Describe the red-green-refactor cycle.”

How to answer

TDD means writing a failing test before the production code, in short cycles. Red: write one small test for the next behavior and watch it fail (in Swift, "doesn't compile yet" counts). Green: write the simplest code that makes it pass. Refactor: clean up names and duplication while all tests stay green, then repeat. The payoff is design pressure: code written test-first naturally takes its dependencies through init and keeps logic in small, pure units.

Key points

  • Watching the test fail first proves it can fail (and that it tests the right thing)
  • One behavior per cycle; green means minimal, even hard-coded, until another test forces generality
  • Refactor is mandatory, not optional; it's where the design improves
  • Swift Testing fits well: parameterized @Test(arguments:) makes adding a case one line

If they dig deeper

  • When does TDD fit best? Business rules, parsers, validators, view model state machines, bug fixes (reproduce with a failing test first). Less so for exploratory UI layout or unfamiliar framework APIs, where a spike comes first.
  • TDD with dependencies: start from the view model test, invent the protocol you wish existed (UserService: Sendable), fake it, then implement the real one with its own tests (outside-in).
  • Fast feedback: keep logic in a Swift package or framework so swift test / a focused test plan runs in seconds without launching the app.

Common pitfalls

  • Writing several tests (or the whole feature) before going green
  • Skipping the refactor step, so the tests pass but the code rots
  • Testing implementation details, which makes refactoring break tests

How to use it

swift
import Testing

// 1. Red: tests first; they fail (initially they don't even compile)
@Suite struct PasswordRuleTests {
    @Test(arguments: ["", "short", "nouppercase1", "NoDigitsHere"])
    func rejectsWeakPasswords(_ password: String) {
        #expect(PasswordRule.isValid(password) == false)
    }

    @Test func acceptsStrongPassword() {
        #expect(PasswordRule.isValid("Correct1Horse"))
    }
}

// 2. Green: the simplest code that passes all cases
enum PasswordRule {
    static func isValid(_ password: String) -> Bool {
        password.count >= 8
            && password.contains(where: \.isUppercase)
            && password.contains(where: \.isNumber)
    }
}

// 3. Refactor: extract named rules, improve messages; tests stay green.
//    Next cycle: add a failing case (e.g. "Password1" is too common) and repeat.

Best practices

  • Start each cycle with one small failing test
  • Write the minimal code to pass, then refactor with tests green
  • Run tests constantly; keep the logic in fast, app-free targets
  • Reproduce every bug with a failing test before fixing it

Related concepts

  • unit testing, Swift Testing, parameterized tests, testability design, given-when-then, test naming, refactoring for testability