Unit test

My notes on unit testing in Xcode

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

UnitTest template:

import XCTest

class SomeUnitTest: XCTestCase {
    override func setUp() {
    override func tearDown() {
    func testExample() {
      XCTAssertEqual("Hello, World!", "Hello, World!")
    func testPerformanceExample() {
        self.measure { }


  • Name the test target with out underscore characters. XCode doesn’t like underscores sometimes


  • 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


  • 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.

Enable parallel tests:

  • To find this option just google: “Execute in parallel on Simulator” (its in edit scheme, test, info, options) Execute in parallel on Simulator. This will run your UI and unit tests in parallel on multiple simulators and speeds up your tests drastically.