/CodeCoverage

Code Coverage in Xcode.

Primary LanguageSwiftMIT LicenseMIT

Code Coverage

YouTube Video Lesson

Code coverage shows you which of your app’s code paths are being tested. When you run a set of tests with code coverage enabled, Xcode tracks how many times each branch in your code is called. The test report shows a hierarchical view of test coverage for each file.

code coverage

Navigation Pane options

navigation pane options

1. Add Unit Test Target

Click on the Test Navigator and add new Unit Test Target. For a higher code coverage aim to write unit tests for all functions you write throughout your application.

2. Add Code Coverage

  1. Navigate to the Scheme Editor.

scheme editor

  1. Click on Edit Scheme.

  2. Select on Test in the left pane. click test and options

  3. Select Options.

  4. Check the box next to Code Coverage and click Close. select gather coverage

3. Report Navigator

  1. Run your tests by clicking on the test or Cmd-U.
  2. Navigate to the Report Navigator.

navigation pane options

  1. Select the lastest Test and click on Coverage to view all the code coverage for you app.

code coverage

4. View controller tests

  func testLoadFellows() {
    // arrange
    // 1
    // first get an instance of the ViewController()
    let viewController = ViewController()
    
    // act
    // 2
    // load the view controller into memory
    viewController.loadViewIfNeeded()
    let fellows = viewController.fellows
    
    // assert
    XCTAssertEqual(fellows.count, 2)
  }

Resources

  1. Wikipedia - Code Coverage
  2. Apple - Code Coverage