I've recently been learning how to write Unit Tests using GHUnit for the iPhone. However, I have n开发者_如何学编程o idea on how to set up code coverage to work with this, via xCode 4.
The googletubes have (somehow) not been particularly helpful in this matter.
I covered how to get code coverage with GHUnit in this blog article (disclaimer: I am the author)
To summarize it briefly, for GHUnit you need to:
- enable the two build settings ‘Generate Test Coverage Files’ and ‘Instrument Program Flow’ in the test target of your project in Xcode
- add the code for fopen$UNIX2003 and fwrite$UNIX2003 functions in the main.m file of your test target, not in the AppDelegate. This is how my main.m file looks like:
#import <UIKit/UIKit.h>
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, @"GHUnitIOSAppDelegate");
}
}
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);
}
- edit your test target .plist and add the property ‘Application does not run in background’ with value ‘YES’. Notice that this is useful because the coverage files are only written when the application exits.
For more detailed explanations/screenshots, see the blog article above. The article is more exhaustive because it also covers Jenkins integration and OCUnit.
There's an answer here that claims to have gotten code coverage working in XCode 4, although I've not verified it:
Code coverage not showing results using Xcode + gcov
精彩评论