Why doesn't gcov report any lines being covered by my unit tests?
Asked Answered
D

5

15

I am using Xcode 3.2 on 10.6, with the shipped version of gcov and default GCC compiler (both version 4.2.1). I have created a dependent Cocoa unit test bundle which is injected into my app, and followed Apple's documentation on setting up a gcov-instrumented build configuration - based on the Debug configuration which doesn't have any compiler optimisations enabled.

When I build the test bundle with this 'Gcov-instrumented' configuration, the app launches and the tests are injected and run. Also the coverage statistics files are generated at:

build/<AppTarget>.build/Gcov-instrumented/<AppTarget>.build/Objects-normal/x86_64/<object>.gcda

build/<AppTarget>.build/Gcov-instrumented/<AppTarget>.build/Objects-normal/x86_64/<object>.gcno

So far so good. I know the tests are really being run because if I insert failures then the test suite fails as expected. Unfortunately, gcov reports that no lines of the objects have been covered by the tests! Every line is reported as 0 coverage. I've searched here and at the Apple mailing list archives, and can't find anyone with a similar problem. I expect I'm missing something - but what is it?

Discourtesy answered 6/9, 2009 at 12:30 Comment(0)
U
10

I have been running into this problem intermittently. I stumbled into a good length of lucky time where it was "mostly working" in my main project. By mostly working I mean I was able to get coverage to show up, but with a little more pain than on 10.5. I had to aggressively delete the coverage files and rebuild in order to get any updates, for instance. Even in my "barely working" state, the compiler/runtime doesn't seem willing to overwrite an existing coverage data file.

I think I've stumbled back on to a working scenario. Voodoo for sure, and less than ideal:

  1. Make sure the SDK is 10.6.
  2. Make sure the deployment is set to gcc 4.0 (!).
  3. Do a full clean of the project.
  4. Rebuild.

Does this get you the expected coverage data?

Granted, I would expect to be able to generate coverage files that work using gcc 4.2 or one of the LLVM compilers, but for the moment this seems to have me slightly less crippled than before.

Another clue: when I observe the coverage data (using Google's Cover Story), I get warnings like:

/BUILDRESULTS/MarsEdit.build/Code Coverage/MarsEditDataModelTests.build/Objects-normal/i386/MEDataItem.gcno:version '400A', prefer '402*'

But the coverage information is still presented. So it seems it's balking at an older coverage format version, but if I use the newer compiler (4.2) which would presumably generate the newer format .gcno files, the 0% coverage problem plagues me as it does Graham.

Unsaddle answered 12/10, 2009 at 17:10 Comment(6)
Thanks for that! I now do get the coverage I'd expect :-)Discourtesy
+1 I got it working with the same approach on Snow Leopard. The requirement to drop to GCC 4.0 is somewhat disconcerting going forward, since code that must be compiled with GCC 4.2 or LLVM (for example, if it uses Blocks) won't work in the old compiler version, and code coverage may disappear. This sounds like a Radar waiting to be filed...Dang
Daniel, I run into similar problems when using lcov to generate HTML coverage reports. It allows me to pass --gcov-tool /usr/bin/gcov-4.0 to override the default of 4.2, which /usr/bin/gcov points to. I peeked at the source of CoverStory, and it's using this symlink. code.google.com/p/coverstory/source/browse/trunk/… Sure would be nice if they did add a user default for this, even if it's not in the preferences dialog...Dang
Quinn, where do you enter the --gcov-tool /usr/bin/gcov-4.0 flags? I've tried this in the linker flags setting, but when I compile, Xcode throws an error at me. When I try to do a gcc compile from the command line with this flag, it also throws an error: ld: warning: in /usr/bin/gcov-4.0, missing required architecture x86_64 in fileIlliquid
@Unsaddle Or I did, until I started using blocks and couldn't use GCC 4.0 :(Discourtesy
@quinntaylor The newest version of Coverstory should determine the gcc version flags for you.Indicia
D
7

In Xcode 4.6, coverage reports just seem to work. Turn on "Generate Test Coverage Files" and "Instrument Program Flow" for the app target, run the tests, then point the coverage tool at the output. That's in ~/Library/Developer/Xcode/Derived Data/<Project>-<hash>/Build/Intermediates/<Target>.build/Debug-iphonesimulator/<Target>.build; opening that folder in CoverStory shows me the results I expected.

This GitHub repository contains scripts that can automatically discover the output and get lcov to visualise it; I'm happy with CoverStory for now.

Discourtesy answered 29/1, 2013 at 13:57 Comment(1)
My path to the .gcda and .gcdo files was slightly different: ~/Library/Developer/Xcode/Derived Data/<Project>-<hash>/Build/Intermediates/<Target>.build/Debug-iphonesimulator/<Target>.build/Objects-normal/i386Tribe
I
2

You can get code coverage working with gcc 4.2.1. Details here:

SnowLeopardGCov

Indicia answered 1/6, 2010 at 22:25 Comment(5)
The contents of that page have been replaced (as of June 4th) with the message “This page is out of date. This information has been consolidated into the Google Toolbox For Mac Wiki.” and a link to code.google.com/p/google-toolbox-for-mac/wiki/SnowLeopardGCov .Fantastic
Fixed up with the new improved link. Thanks Peter.Indicia
that didn't work for me. I set the compiler to 4.2, removed the prefix headers and did an entirely clean build of the unit test target. I don't get any coverage information at all.Discourtesy
Hey Graham. I assume you also turned on the "Instrument Program Flow" and "Generate Test Coverage Files" flags for the target?Indicia
Graham, please make sure you are quitting correctly with iOS 4.0. more info here: code.google.com/p/google-toolbox-for-mac/wiki/iOSGCovIndicia
T
0

I have been trying to get the Code coverage working for iPhone simulator and always get a 0% coverage. Below are the configuration details and the steps that I have tried.

Configuration

Xcode 3.2.5/iOS 4.1 and iOS 4.2/Mac 10.6/GCC 4.2 Application UICatalog

References

http://www.cubiclemuses.com/cm/articles/2009/05/14/coverstory-on-the-iphone/

http://developer.apple.com/library/mac/#qa/qa2007/qa1514.html

Steps

  • Enable “Generate Test Coverage Files”
  • Enable “Instrument Program Flow”
  • Add “-lgcov” to “Other Linker Flags”
  • UIApplicationExitsOnSuspend flag in Info.plist is set to true

Result

I have the .gcda files generated but the coverage always show 0%.

Settings tried

  1. Changing GCC to 4.0 and 4.2. When I try to change the GCC to 4.0 I get 26 build errors.
  2. Set environment variables:

    const char *prefix = "GCOV_PREFIX";
    const char *prefixValue = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] cStringUsingEncoding:NSASCIIStringEncoding]; // This gets the filepath to the app's Documents directory
    const char *prefixStrip = "GCOV_PREFIX_STRIP";
    const char *prefixStripValue = "1";
    setenv(prefix, prefixValue, 1); // This sets an environment variable which tells gcov where to put the .gcda files.
    setenv(prefixStrip, prefixStripValue, 1); // This tells gcov to strip the default prefix, and use the filepath that we just declared.)
    
  3. GCC Optimization set to None (-O0) and unchecked the precompiled prefix header file flag.
Twitt answered 24/2, 2011 at 6:3 Comment(0)
V
0

I was getting 0 coverage when *.gcno files for static library was overwritten by those for shared library.

Vesiculate answered 21/9, 2011 at 20:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.