Swift Code Coverage: What does that mean?

In this lesson

In this lesson, you will learn what code coverage is as it relates to tests. See how XCode allows you to see the code coverage by class and function and displays useful information in the gutter next to your source code. This allows you to see what code is covered by your tests and what code is not.


Tap on time to skip ahead


In this lesson, I’m going to talk about code coverage. What exactly is code coverage? Here, I’m using Swift for iOS. You could be using anything, the concepts are the same. I have a framework that I’ve written here. This is a framework that wraps itself around a SQLite database and interacts with it. I have a little bit of code here. I also have some tests. I wrote some integration tests that verify that that component, or these components, interact properly with the database. I have several different tests here. I want to use code coverage to measure how well my tests cover the code that I’ve written in the framework.


Let’s head over and run the tests and collect some code coverage data. I’ve selected the test navigator button. I’m selecting the test that I want to run, click the run button and my IDE will compile and run the tests. We can see that they ran.


Let’s head over to the report navigator where I can look at some data about the tests that ran. They all passed. There’s quite a few, you can see. I’m going to select the Coverage link here in the report navigator. Conveniently, my IDE has collected and displayed data about the code coverage. In this case, if I hover over, it shows that 55% of the lines in this framework were touched during the execution of the tests. If we scroll down here, I can look at all of the classes in the framework and the individual functions and I can look at the code coverage for each of those functions, which is very convenient.


55% looked good, but if we look a little deeper, we can see that there’s entire functions that aren’t touched at all. As a developer, I can use this tool to determine if that’s good enough or not. Some functions may be getters and I don’t really care about testing those. But it looks like some of this code down here probably needs tests. I can use this tool here to determine that.


In fact, if I want to dive a little deeper, I can hover over this arrow here and my IDE will take me directly into the code and you can see over here in the gutter that the IDE has annotated my source code to show me the coverage data that was collected. It’s very convenient. It allows me to easily hover and see what code is covered and not. The red obviously means that this code was not touched during the execution of my tests. The number in the gutter displays how many times this code was executed. If it’s red, obviously that means zero. Green, this code was executed 27 times during the tests. This code here, 56 times.


I can kind of scroll down through here and interactively look at the code and the coverage data and see what was not covered. Interestingly, this function, this line here was touched, this bit of code was touched 54 times, this only once. I can kind of scroll through and use this coverage data annotation to quickly help me determine where I need to write more tests. If we scroll down here, we can notice that this is the area in the report that showed i didn’t have very good coverage. So, I may decide to write more tests against this code down here.

Additional Info

Register to get access to additional resources and info.