Test-Driven Development: Write the Test First
TDD flips the script: write a failing test before the feature code, forcing a testable design. It's ideal for new Swift components with clear requirements, ensuring every line of code serves a purpose.
Why it exists
Traditional development can lead to code that's difficult or impossible to test after the fact. TDD was created to solve this by making testability a primary constraint on design, not an afterthought. It forces you to think about your code's public interface and expected behavior before you write a single line of implementation.
The mental model
Think of TDD as building with a blueprint and a quality inspector at every step. The failing test is the blueprint for the next small piece you need to build. The passing test is the inspector signing off on that piece. The refactoring step is cleaning up the construction site before starting the next piece. This cycle is often called "Red-Green-Refactor".
How it works
The process is a tight, repeating loop. First, you write a single, small, automated unit test for a new feature. This test must fail because the feature doesn't exist yet (the "Red" phase). Second, you write the absolute minimum amount of production code required to make that specific test pass (the "Green" phase). Third, with the safety of a passing test, you refactor both the production code and the test code to improve clarity and remove duplication. Then you repeat the cycle with the next small requirement.
When to use it
TDD shines when you are building new functionality with well-defined inputs and outputs, like a data parsing utility, a complex business logic engine, or a custom algorithm in Swift. It's also valuable when fixing a bug; you first write a test that reproduces the bug (it will fail), then you write the code to fix it (making the test pass).
When not to use it
TDD can be less effective for exploratory coding where the final design is unknown, such as rapid prototyping of a UI. It can also be cumbersome for code that is difficult to unit test in isolation, like complex view controller logic or code that relies heavily on external system frameworks. Don't force TDD on a legacy codebase without a clear strategy for introducing tests safely.
One canonical example
Imagine creating a PasswordValidator struct in Swift. Using TDD, you'd first write a test in XCTest: testPasswordIsTooShort(). You'd assert that validator.isValid("123") is false. This test fails because PasswordValidator doesn't exist. You then create the struct and method with just enough logic to make the test pass (e.g., return password.count >= 8). Now the test is green. Next, you might refactor. Then you'd write a new failing test, testPasswordIsLongEnough(), and repeat the cycle.
Interview question
What is the main benefit of writing a failing test before implementing a new feature in TDD?
- a.It ensures the new feature's design is inherently testable and focused on clear requirements.Correct
- b.It immediately identifies and fixes bugs present in existing code.
- c.It allows for immediate refactoring of the codebase before adding any new logic.
- d.It provides comprehensive documentation for the entire application's functionality.
Why? this is the answer
The card states TDD "forces you to think about your code's public interface and expected behavior before you write a single line of implementation" and makes "testability a primary constraint on design." This aligns with option A. Option B is incorrect because the initial failing test in TDD is for a *new* feature, not to find existing bugs in other code.
Just read this? Test yourself on what you have been reading.
Read the original → en.wikipedia.org
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on swift — each one lists the topics its interview covers.
See open roles