Long long ago, I wrote a post about how to do TDD using
Objective-C, since Apple WWDC 2014,
Swift is really
eye-catching, I think I should write a new one to follow the trend.
XCTest
is used as the unit test framework, and Xcode 6 is needed.
TDD Work-flow
- Add a test for a user case or a user story
- Run all tests and see if the new one fails
- Write some code that causes the test to pass
- Run tests, change production code until all test cases pass
- Refactor the production code
- Refactor the test code
- Return to 1, and repeat
The 5
and 6
are optional, do them only if
needed, but be sure that DO NOT do them at the same time. That is, when
you refactor production code, you can't change the test code, until all
the test cases are passed, then you are confident that your production
code refactoring is perfect, then, you can refactor the test code, and
this time, you can't change the production code.