Gaining insights through code coverage

Code Coverage is a tool in Xcode that is used to gain insights into how much of your code you are testing with your test suite. It tells you exactly which parts of your code were executed during a test and which parts of your code were not. This is extremely useful because you can take focused action based on the information provided by Code Coverage.

To enable Code Coverage, open the scheme editor through the (Product | Scheme) menu:

Select the Test action and make sure the Gather coverage checkbox on the Options tab is checked:

You can also press Cmd + to open the scheme editor quickly.

After doing this, close the scheme editor and run your tests. This time, Xcode will monitor which parts of your code were executed during this test, and which parts weren't. This information can give you some good insights about which parts of your code could use some more testing. To see the coverage data, open the Report navigator in the left sidebar in Xcode. The rightmost icon in this sidebar represents the Report navigator:

There are several reports listed under your app name. If you select the Coverage report, the coverage report will open in the Editor Area in Xcode. You can see all the files in your app and the percentage of code in the file that's covered by your tests. The following screenshot shows Coverage for the MovieTrivia app:

The more a bar is filled, the more lines of code in that file or method were executed during your test. You'll notice that the AppDelegate.swift file is covered under the tests even though you haven't written any tests for it. The reason this happens is that the app must launch during the test to act as a host for the test suite. This means that parts of the code in AppDelegate.swift are actually executed during the test, and therefore Xcode considers it covered in the tests.

You can see which methods for a specific file were executed by clicking on the triangle next to the class name. This enables you to see exactly which parts of a file are tested and which parts aren't.

One last feature of Code Coverage that's worth mentioning is inline Code coverage. Inline Code coverage will show you how often a specific block of code has been executed during testing. This will give you code coverage insights right next to your code, without having to navigate to the Reports navigator. To enable this feature, open up your Xcode preferences and navigate to the Text Editing tab. Check the Show iteration counts checkbox at the bottom of the tab. If you open a file now, you'll see the iteration count for your code on the right side of the editor window. The following screenshot shows the iteration count for the loadQuestions(callback:) method:

Even though Code Coverage is a great tool for gaining insights into your tests, you shouldn't let it influence you too much. Regularly check the Code Coverage for your app and look for methods that are untested and are either easy to write tests for, or should be tested because they contain important logic. Code Coverage is also great for discovering parts of your code that should be tested but are hard to test because they're nested deep inside a view controller or otherwise hard to reach.

You should always aim for as much code coverage as possible, but don't push yourself to reach 100%. Doing this will make you jump through all kinds of hoops, and you'll invest way more time in testing than you should. Not all paths in your code have to be tested. However, don't shy away from doing some refactoring. Proper testing helps you to avoid bugs and to structure your code better. Code Coverage is just one extra tool in your tool belt to help identify which parts of your code could benefit from some tests.

If you look at the current state of the coverage in the MovieTrivia app, we're doing quite well. Most of the logic in the app is tested. The only parts that are not tested thoroughly are the view controllers. Testing view controllers and navigations flows with XCTest can be quite hard and tedious. Luckily, there is one last testing tool that we'll discuss in this chapter: XCUITest.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset