Generate gcda-files with Xcode5, iOS7 simulator and XCTest

前端 未结 6 1260
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 06:09

Being inspired by the solution to this question I tried using the same approach with XCTest.

I\'ve set \'Generate Test Coverage Files=YES\' and \'Instrument Program

6条回答
  •  抹茶落季
    2020-12-02 06:34

    Here's another solution that avoids having to edit your AppDelegate

    UIApplication+Instrumented.m (put this in your main target):

    @implementation UIApplication (Instrumented)
    
    #ifdef DEBUG
    
    + (void)load
    {
        NSString* key = @"XCTestObserverClass";
        NSString* observers = [[NSUserDefaults standardUserDefaults] stringForKey:key];
        observers = [NSString stringWithFormat:@"%@,%@", observers, @"XCTCoverageFlusher"];
        [[NSUserDefaults standardUserDefaults] setValue:observers forKey:key];
    }
    
    - (void)xtc_gcov_flush
    {
        extern void __gcov_flush(void);
        __gcov_flush();
    }
    
    #endif
    
    @end
    

    XCTCoverageFlusher.m (put this in your test target):

    @interface XCTCoverageFlusher : XCTestObserver
    @end
    
    @implementation XCTCoverageFlusher
    
    - (void) stopObserving
    {
        [super stopObserving];
        UIApplication* application = [UIApplication sharedApplication];
        SEL coverageFlusher = @selector(xtc_gcov_flush);
        if ([application respondsToSelector:coverageFlusher])
        {
            objc_msgSend(application, coverageFlusher);
        }
        [application.delegate applicationWillTerminate:application];
    }
    
    @end
    

提交回复
热议问题