title: Generate gcda-files with Xcode5, iOS7 simulator and XCTest
body: <p>Being inspired by the <a href="https://stackoverflow.com/questions/18394655/xcode5-code-coverage-from-cmd-line-for-ci-builds">solution to this question</a> I tried using the same approach with XCTest.</p>
  
  <p>I've set 'Generate Test Coverage Files=YES' and 'Instrument Program Flow=YES'.</p>
  
  <p>XCode still doesn't produce any gcda files. Anyone have any ideas of how to solve this?</p>
  
  <p>Code:</p>
  
  <pre><code>#import &lt;XCTest/XCTestLog.h&gt;
  
  @interface VATestObserver : XCTestLog
  
  @end
  
  static id mainSuite = nil;
  
  @implementation VATestObserver
  
  + (void)initialize {
      [[NSUserDefaults standardUserDefaults] setValue:@"VATestObserver"
                                               forKey:XCTestObserverClassKey];
      [super initialize];
  }
  
  - (void)testSuiteDidStart:(XCTestRun *)testRun {
      [super testSuiteDidStart:testRun];
  
      XCTestSuiteRun *suite = [[XCTestSuiteRun alloc] init];
      [suite addTestRun:testRun];
  
      if (mainSuite == nil) {
          mainSuite = suite;
      }
  }
  
  - (void)testSuiteDidStop:(XCTestRun *)testRun {
      [super testSuiteDidStop:testRun];
  
      XCTestSuiteRun *suite = [[XCTestSuiteRun alloc] init];
      [suite addTestRun:testRun];
  
      if (mainSuite == suite) {
          UIApplication* application = [UIApplication sharedApplication];
          [application.delegate applicationWillTerminate:application];
      }
  }
  
  @end
  </code></pre>
  
  <p>In AppDelegate.m I have:</p>
  
  <pre><code>extern void __gcov_flush(void);
  - (void)applicationWillTerminate:(UIApplication *)application {
      __gcov_flush();
  }
  </code></pre>
  
  <p><strong>EDIT</strong>: I edited the question to reflect the current status (without the red herrings).</p>
  
  <p><strong>EDIT</strong> To make it work I had to add the all the files under test to the test target including VATestObserver.</p>
  
  <p>AppDelegate.m</p>
  
  <pre><code>#ifdef DEBUG
  + (void)initialize {
      if([self class] == [AppDelegate class]) {
          [[NSUserDefaults standardUserDefaults] setValue:@"VATestObserver"
                                                   forKey:@"XCTestObserverClass"];
      }
  }
  #endif
  </code></pre>
  
  <p>VATestObserver.m</p>
  
  <pre><code>#import &lt;XCTest/XCTestLog.h&gt;
  #import &lt;XCTest/XCTestSuiteRun.h&gt;
  #import &lt;XCTest/XCTest.h&gt;
  
  // Workaround for XCode 5 bug where __gcov_flush is not called properly when Test Coverage flags are set
  
  @interface VATestObserver : XCTestLog
  @end
  
  #ifdef DEBUG
  extern void __gcov_flush(void);
  #endif
  
  static NSUInteger sTestCounter = 0;
  static id mainSuite = nil;
  
  @implementation VATestObserver
  
  + (void)initialize {
      [[NSUserDefaults standardUserDefaults] setValue:@"VATestObserver"
                                               forKey:XCTestObserverClassKey];
      [super initialize];
  }
  
  - (void)testSuiteDidStart:(XCTestRun *)testRun {
      [super testSuiteDidStart:testRun];
  
      XCTestSuiteRun *suite = [[XCTestSuiteRun alloc] init];
      [suite addTestRun:testRun];
  
      sTestCounter++;
  
      if (mainSuite == nil) {
          mainSuite = suite;
      }
  }
  
  - (void)testSuiteDidStop:(XCTestRun *)testRun {
  
      sTestCounter--;
  
      [super testSuiteDidStop:testRun];
  
      XCTestSuiteRun *suite = [[XCTestSuiteRun alloc] init];
      [suite addTestRun:testRun];
  
      if (sTestCounter == 0) {
          __gcov_flush();
      }
  }
  </code></pre>