Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage result is not accurate to real coverage in Xcode 7

Tags:

I am running test cases in application with enabled code coverage data Xcode 7 Beta 2. But I am able to get only few files coverage data while my all test cases are running successfully.

Some files has covered all codes by unit test cases but still showing 3% code coverage.

For example:

enter image description here This is the result of code coverage, as you can see on the right side, there is an info how many times these lines of code was called during tests. In this case - 0.

But...

enter image description here here is a place in tests where we can see that this function was called indeed. How many times? oh... at least once. This number is delivered by info on the right side.

So the code above should be marked as called, and not be grayed out:-)

Can anyone explain this? Why does this happen?

like image 281
Sudhir Kumar Avatar asked Jun 22 '15 09:06

Sudhir Kumar


People also ask

How does Xcode measure code coverage?

To measure and visualize code coverage for a project, follow these steps: Enable code coverage date gathering. To do this, go to Product › Scheme › Edit Scheme... , and select Test from the left hand side menu. Under the Info section, check the Gather coverage data box.

How do I show test coverage in Xcode?

To view the coverage reports: Select the Report Navigator in the navigator pane on the left (⌘8) Select the latest Test run in the navigator pane. Select the Coverage tab in the editor.

How do I increase code coverage in Xcode?

Enabling Code Coverage in Xcode Code coverage is enabled in the scheme editor. Click the Covered scheme and choose Edit Scheme.... Select Test on the left and check the checkbox Gather coverage data. That is it.

How do I verify code coverage?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.


1 Answers

IT WORKS.

  1. Since Apple released @testable keyword to import your project into test target, you don't have to add your files to both target anymore:

enter image description here

  1. So just remove every file from your test target:

enter image description here

  1. Wherever you need access to your file from your test target just import your target using: @testable import MyApp

enter image description here

  1. Do this for every file in your project.

Then code coverage will be working fine.

Read more from Swift 2 + Xcode 7: Unit Testing Access Made Easy!!!!

If you need to know how to work with code coverage read How to use code coverage in Xcode 7?

As @Gerd Castan mentioned earlier is: "So it appears to me that a tested method shows a coverage of 0 when there exists at least one target where this method is not tested."

Solution is simple. Do not let compiler think that this file is included in more that one target, instead import your module using @testable keyword.

like image 95
Bartłomiej Semańczyk Avatar answered Oct 04 '22 13:10

Bartłomiej Semańczyk