Generating a coverage report
When you run all the unit tests again, a code coverage report will be generated:
- Go to the Reports Navigator in Xcode and click on the { } Coverage item of the latest test:
2. A list of modules and functions is displayed:
If you expand the item list of App.framework, you'll see the percentage of coverage of your unit tests.
3. Double-click on the routes.swift file and Xcode will take you to the source editor:
You'll notice that some code is highlighted in red to bring your attention to them. They are the lines of code that haven't been executed by your tests.
The lines of code that have been executed before are highlighted in green, with the number of executions listed on the right. For example, in our testStudent()unit test, the "student" route is executed five times. It corresponds to the number of element pairs in studentRecords[].
Your goal is to increase the code coverage by adding additional testing logic that invokes the highlighted code missed in your previous tests.
In ensuring the proper coverage of unit tests in your project, you want to have every public method covered by at least one corresponding test. Each time, after writing a test for each public method, you can run the tests again and check the coverage.
If you follow this approach to cover as much code as possible, you're going to bring the total percentage of test coverage up. It is a very good habit for you to add new unit tests right after each new feature you have added to the project. Typically, a developer spends twice the amount of time on writing tests or debugging than writing the code. By adding unit tests incrementally and in a disciplined fashion, you will be eventually saving more time by reducing the number of defects occurring during runtime.