My notes on unit testing in Xcode
⚠️️ (This article is being updated in the coming days, with video and more advance case studies) ⚠️️
Xcode unit testing
- Under targets, add target, add unit test
- In the
...Test/...Tests.swiftfile inside the
XCTAssertEqual("Hello, World!", "Hello, World!")
- Product -> Test (cmd+U) alternatively: right click the
...Tests.swiftand click: Run …Tests
- Writing tests first gives us a clear perspective on the API design, by getting into the mindset of being a client of the API before it exists.
- Good tests serve as great documentation of expected behaviour.
- some tests for core functionality is good
- hard to pivot after many tests are written
- hard to reorg code
- high code coverage makes code bureaucratic
RGR ( Red — Green — Refactor ) methodology
Red, Green and Refactor are stages of the TDD (Test Driven Development).
- Red: Write a small amount of test code usually no more than seven lines of code and watch it fail.
- Green: Write a small amount of production code. Again, usually no more than seven lines of code and make your test pass.
- Refactor: Tests are passing, you can make changes without worrying. Clean up your code.