Code Coverage in XCode 4.3

405 views
Skip to first unread message

affogato

unread,
Mar 5, 2012, 5:48:41 AM3/5/12
to Australian Cocoaheads
Has anyone found a neat way to test code coverage in Xcode 4.3?

Jasper Blues

unread,
Mar 5, 2012, 6:00:55 AM3/5/12
to cocoah...@googlegroups.com
Yep - don't link to libprofile_rt.dylib, because if you set the following settings: 

-- Instrument Program Flow : YES
--Generate Test Coverage Files : YES

. . . then it will link to the lib profile for you. 

Also (important), there's a bug in a function mapping that can be corrected by adding a class to you project like so: 

#import <Foundation/Foundation.h>

/**
* This fixes a problem related to an Apple implementation of a core unix api.
*/
@interface CoverageFix : NSObject


FILE* fopen$UNIX2003(const char* filename, const char* mode);

size_t fwrite$UNIX2003(const void* ptr, size_t size, size_t nitems, FILE* stream);

@end

#import "CoverageFix.h"


@implementation CoverageFix

FILE* fopen$UNIX2003(const char* filename, const char* mode) {
    return fopen(filename, mode);
}

size_t fwrite$UNIX2003(const void* ptr, size_t size, size_t nitems, FILE* stream) {
    return fwrite(ptr, size, nitems, stream);
}

@end


Having done that you'll have .gcda files in your build products directory. You can turn them into a report with Cover Story, but I like to use lcov to produce HTML reports. The advantage of this is: 

1. A cmd-line build that can be run either on the devs machine or on the CI server, with same setup. 
2. Ability to add coverage assertions - so that the build fails with insufficient coverage. 

There's an example of a build that does this here: https://github.com/expanz/xcode-editor/blob/master/build.xml



Let me know if you need any help. 

Jasper


On Mar 5, 2012, at 6:48 PM, affogato wrote:

Has anyone found a neat way to test code coverage in Xcode 4.3?

--
You received this message because you are subscribed to the Google Groups "Australian Cocoaheads" group.
To post to this group, send email to cocoah...@googlegroups.com.
To unsubscribe from this group, send email to cocoaheadsau...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cocoaheadsau?hl=en.


Stewart Gleadow

unread,
Mar 5, 2012, 6:03:21 AM3/5/12
to cocoah...@googlegroups.com
I tried to get it up and running, but it fails because libprofile_rt can't be found. I looked inside the new Developer dir inside Xcode.app, and I can't see the lib directory where it used to sit...

- Stew


On 05/03/2012, at 9:48 PM, affogato wrote:

> Has anyone found a neat way to test code coverage in Xcode 4.3?
>

Stewart Gleadow

unread,
Mar 5, 2012, 6:05:49 AM3/5/12
to cocoah...@googlegroups.com
Ah nice. I should have been slower with my reply. I didn't realise the libprofile_rt link happened for you. I'm going to try this tomorrow.

- Stew

Jasper Blues

unread,
Mar 5, 2012, 6:06:31 AM3/5/12
to cocoah...@googlegroups.com
It's in here: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib

. . . but I found if you just set the two parameters (not the C flags) (see my other email) its more stable. Not sure why. . .

Reply all
Reply to author
Forward
0 new messages