Meesh
Meesh

Reputation: 379

Unable to see Code Coverage in Xcode 4.2

I'm creating an iPhone 5.0 project in Xcode 4.2 and would like to find the code coverage when the unit tests are executed. I'm quite new to the Xcode environment, and I've followed the steps provided here. I'm able to modify the Build Settings for the test target correctly, and link the "libprofile_rt.dylib" file fine.

At this point, when I execute the tests (using Command-U), the code compiles and the tests pass. I do not encounter the problem described here. In addition, I've installed CoverStory.

The author in the first link mentions "Just run your unit tests and view the code coverage data as usual"; however, I cannot find .../Objects-normal/i386.

Just to get things working, I created a new project with the following class:

#import "SomeClass.h"

@implementation SomeClass
@synthesize someValue;

-(void)performWork:(BOOL)now withValue:(int)value {
    if (now) {
        someValue = value;
    }
    else {
        someValue = value - 1;
    }
}

@end

and test class:

#import "CodeCoverageTests.h"
#import "SomeClass.h"

@implementation CodeCoverageTests

- (void)testExample {
    SomeClass *obj = [[SomeClass alloc] init];
    [obj performWork:YES withValue:3];
    STAssertEquals(obj.someValue, 3, @"Value was not 3");
}
@end

Ideally, I'd like to be notified in some way that when the tests execute, the else clause in the performWork method is never fired.

I thus have the following questions:

  1. Is the root problem that there's no support for what I'm trying to do with the new compiler?
  2. Is the only solution the one described by user chown in response to the question I linked above?
  3. Will I be able to use CoverStory (or something similar) if I follow the solution from 2) ?

Update: After some struggle, I was finally able to find the location of the "SomeClass.gcno" and "SomeClass.gcda" files (thanks @bjhomer - see this link), and they depicted beautifully that the if part of the conditional statement in performWork was covered (and the else was not). To make sure, I modified the test as follows:

- (void)testExample
{
    SomeClass *obj = [[SomeClass alloc] init];
    [obj performWork:NO withValue:3];
    STAssertEquals(obj.someValue, 2, @"Value was not 2");
}

After re-building and re-execution of the unit test, I reloaded the .gcno and .gcda files. CoverStory showed that the coverage changed to the else part of the performWork method. There was one small caveat however:

  1. I needed to modify the build settings of the <TargetName> (not the <TargetNameTest> as shown here) in order for the "SomeClass.gcno" and "SomeClass.gcda" files to be created in ...<TargetName>.build/Objects-normal/i386/ directory.

Thanks again for your help!

Upvotes: 4

Views: 559

Answers (1)

BJ Homer
BJ Homer

Reputation: 49054

It sounds like your main problem is that you cannot find the Build/Intermediates/<SchemeName>.build/<ConfigurationName>-<PlatformName>/<TargetName>.build/Objects-normal/<ArchitectureName> directory. That directory is always used when building, whether or not you're doing code coverage. That's where all the intermediate files generated by the compiler are stored, and is also where the final .gcno and .gcda files should exist if you're doing code coverage. If you cannot find that directory, the problem is not related to coverage, the problem is that you need to find that directory.

To answer your questions.

  1. Code coverage is supported in the LLVM Compiler, as of Xcode 4.2. It will not work with LLVM-GCC, so make sure you're not using that.
  2. No, it is not necessary to hack in support for GCC 4.2 in Xcode 4.2. I'm running code coverage with Xcode 4.2 right now, having done none of the above.
  3. You should be able to use CoverStory either way; hacking in support for GCC 4.2 would not change where the built products go.

Upvotes: 1

Related Questions