Unit test


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

  1. Under targets, add target, add unit test
  2. In the ...Test/...Tests.swift file inside the testExample method add XCTAssertEqual("Hello, World!", "Hello, World!")
  3. Product -> Test (cmd+U) alternatively: right click the ...Tests.swift and click: Run …Tests

Pro

  • 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

Con

  • hard to pivot after many tests are written
  • hard to reorg code
  • high code coverage makes code bureaucratic

Resources:

  • https://www.raywenderlich.com/101306/unit-testing-tutorial-mocking-objects

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.