-

Test Coverage

How to get test coverage on Xcode?

Using a coverage tool allows you to immediately see gaps in your testing strategy. Xcode has a built in coverage tool which at the time of writing is not enabled by default.

Scheme settings screenshot
Enable code coverage by going to your scheme -> edit schemes -> Test -> Options and check Gather coverage
Test coverage report screenshot
View your projects coverage within the reports tab cmd-9
Xcode untested code highlighting screenshot
Xcode will highlight untested functions with a red margin

A warning on Code coverage

Code coverage reports are great but don’t treat them as the source of all truth. Just because a function has test coverage doesn’t mean it’s fully tested.

Your tests should be testing the functionality of your code not the functions. Push your code to the limit and write tests for every edge case you can think of. It’s better to have too many tests than too few.

Let’s take an example from a previous post where we have an addMonths(_: Int) function that adds a month to the current date. One test that calls addMonths will satisfy code coverage tools that the function is tested but there is far more behavior to be tested than just calling this function once.

Rather than just test the function addMonth() we should be testing

On the other side of the coin I wouldn’t suggest always striving for 100% coverage. Xcode will often want coverage for things such as enums cases or state with no functionality which are likely not worth your time.