Xcode5 Code Coverage (from cmd-line for CI builds)

后端 未结 6 1596
广开言路
广开言路 2020-12-14 03:48

How can I generate code coverage with Xcode 5 and iOS7?

Prior to upgrading I was getting code coverage just fine. Now I can\'t see any *.gcda files being produced.

6条回答
  •  遥遥无期
    2020-12-14 04:29

    The following is a fix for SenTestKit - simply add this class to your Tests target. Something similar should be possible to do with XCTest

    @interface VATestObserver : SenTestLog
    
    @end
    
    static id mainSuite = nil;
    
    @implementation VATestObserver
    
    + (void)initialize {
        [[NSUserDefaults standardUserDefaults] setValue:@"VATestObserver" forKey:SenTestObserverClassKey];
    
        [super initialize];
    }
    
    + (void)testSuiteDidStart:(NSNotification*)notification {
        [super testSuiteDidStart:notification];
    
        SenTestSuiteRun* suite = notification.object;
    
        if (mainSuite == nil) {
            mainSuite = suite;
        }
    }
    
    + (void)testSuiteDidStop:(NSNotification*)notification {
        [super testSuiteDidStop:notification];
    
        SenTestSuiteRun* suite = notification.object;
    
        if (mainSuite == suite) {
            UIApplication* application = [UIApplication sharedApplication];
            [application.delegate applicationWillTerminate:application];
        }
    }
    

    and add

    extern void __gcov_flush(void);
    
    - (void)applicationWillTerminate:(UIApplication*)application {
        __gcov_flush();
    }
    

    Why is this working?

    Tests and the tested application are compiled separately. Tests are actually injected into the running application, so the __gcov_flush() must be called inside the application not inside the tests.

    The little magic with the observer only enables us to check when the tests are going to end and we trigger __gcov_flush() to be called inside the app.

提交回复
热议问题