Xcode 9.3 Code Coverage

I'm in the early stages of a project and code coverage is fairly important. When I was using Xcode 9.2, the editor would correctly show code coverage. However, code coverage reports were always empty.


I just updated to Xcode 9.3 and now the editor does not show any code coverage; that is, code coverage ribbons are always red and contain a 0. In addition,code coverage reports are empty.


I have tried to building Clean and Cleaning Build Folder. I have tried turning off everything related. to coverage and turning it back on. About the only thing I haven't done is create a new project and move all the source into it.


Does anyone have any ideas? Is this ia legitimate problem? If so,I'll submit a bug report.


It might be worth mentioning that this is a Command Line Tool project.


Cheers,

-Patrick

Replies

Regardless of everything below, this is definitely bugworthy. Either Xcode is misbehaving, and that obviously warrants a bug, or you’re doing something wrong, and that warrants a bug to improve the UI, warnings, or whatever.

It might be worth mentioning that this is a Command Line Tool project.

OK. How have you set up your unit tests? There’s two standard ways to approach unit testing a command-line tool:

  • Put the core code in a framework and have the unit tests target that framework.

  • Put a copy of the code code into the unit test bundle itself (in this case you need to select the Show Test Bundles check box in the code coverage editor.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi Eskimo,


I included a copy of the code in the unit test bundle. I did enable Show Test Bundles, but no joy.


To make progress, I created a Cocoa project and copied the code into it. Xcode seems to behave properly for Cocoa projects, both in the editor and the coverage.reporter.


I have submitted bug report 39138509.


One thing that is odd. When creating a project to illustrate the problem, I accidentally forgot to add the test target to the test scheme. Doing this, I was able to individually execute tests and coverage seemed to work as expected. However, as soon as I added the test target to the test scheme, coverage stopped working.


Cheers,

-Patrick

Hello eskimo,


It seems like code coverage reporting is totally broken in new Xcode 9.3. I have project that is widely tested and covered, that was properly working under Xcode 9.2, but after upgrading to new version(9E145), I'm unable to see those coverage reports.


I've also tried to create entirely new empty macOS project with both unit and UI test bundles, and was not able to see a test coverage reports after running all tests.


I've submitted two bugs conserning this to https://bugreport.apple.com/.See #39138502 and #38615865 for more information.


Hope this will help to resolve this obvious bug :)

Hi Nataliia,


It looks like code coverage is off by default in 9.3. You need to enable it in the scheme editor. Unfortunately, the "Enable code coverage" section of the Xcode Help docs is a little out-of-date. Here is the process:


1. Choose Edit Scheme from the scheme menu in the toolbar.


2. In the left column, select the Test scheme action.


3. Click Options at the top of the right column.


4. Click the “Gather coverage for” check box and set the popup to either "all targets" or "some targets."


5. Click Close.


After doing this for a new iOS framework project, code coverage worked as expected for me. The Editor > Show Code Coverage command shows the results in the right margin.

It looks like I lost concentration. Indeed, after turning "Gather coverage for" on, I'm able to see the coverage reports.


However, this doesn't work for out main project. After migrating to new version of Xcode, this option("Gather coverage for") is enabled for "All Targets" and it does not appear to be working.

I have the same problem 😟

I have the same issue with a Xcode server build; the code coverage is not showing up. I did manage to get it to show up once but that seemed to be a fluke. I did report this during beta but at the time I had already deleted it from my server and could not send logs. I have since sent logs and logged a bug (#39045011 on 3/30/18/).


I really hope is fixed soon and Apple releases a 9.3.1.

I have the same problem with a framework. None of the suggestions here worked, but I did notice something interesting--


I get code coverage on some Objective C and some C code. Just nothing for Swift. I've upgraded (?) to it all to Swift 4.1.

I've got a similar issue where i'm getting code coverage across most of my Swift code (and ObjC) but some of the initialisers that are definitely getting covered are being reported as zero coverage. It seems like it might be related to convenience initialisers and overriden initialisers called by those initialisers not getting their coverage reported e.g.:


class CustomAVPlayerItem: AVPlayerItem {


override init(asset: AVAsset, automaticallyLoadedAssetKeys: [String]?) {

// Incorrect zero coverage

super.init(asset: asset, automaticallyLoadedAssetKeys: automaticallyLoadedAssetKeys)

}


convenience init(url: URL) {

// Correct coverage

let asset = AVURLAsset(url: url)

self.init(asset: asset)

}

}


As you can see from the example above the url init calls the asset init (confirmed with breakpoints) so both should be reported as covered, but only the url init gets coverage reported.

Everything is set. Somehow it works. but does not show even a half of the real coverage.

I am seeing the same problem in 9.4, but I found a way to workaround it. If you edit the scheme, and select "some targets" for the "Gather coverage for" option, then manually select all your targets, it will work.


It doesn't seem to work with the default "all targets" option though, possibly in the case you have multiple projects in your workspace.


Matt.

I'm curious. When you changed your scheme to "some targets", did you then add a specific target to the list? If nothing is added in there, no test coverage will be gathered and the right side of the edit window won't show any red, which could be mistaken for 100% coverage. The only way to be sure is to open the Report Navigator, find the most recent Test entry, expand it, and click on the "{ } Coverage" row. If there is any coverage collected, it will show here.