#endif

Previously, this answer suggested to use the +initialize method (and you can still do that in case of Apps) but it doesn't work for libraries…
In the case of a library, the +initialize will probably be executed only when the tests invoke the library code for the first time, and by then it's already too late to register the observer. Using the +load method, the observer registration in always done in time, regardless of which scenario.
In the case of Apps, add the following code to the @implementation section of the ExampleAppDelegate.m file, inside the ExampleApp group, to flush the coverage files on exiting the app:
- (void)applicationWillTerminate:(UIApplication *)application
{
#ifdef DEBUG
    extern void __gcov_flush(void);
    __gcov_flush();
#endif
}

Enable Generate Test Coverage Files and Instrument Program Flow by setting them to YES in the project build settings (for both the "Example" and "Example Tests" targets).
To do this in an easy and consistent way, I've added a Debug.xcconfig file associated with the project's "Debug" configuration, with the following declarations:
GCC_GENERATE_TEST_COVERAGE_FILES = YES
GCC_INSTRUMENT_PROGRAM_FLOW_ARCS = YES

Make sure all the project's.m files are also included in the "Compile Sources" build phase of the "Example Tests" target. Don't do this: app code belongs to the app target, test code belongs to the test target!

After running the tests for your project, you'l be able to find the generated coverage files for the Example.xcodeproj in here:
cd ~/Library/Developer/Xcode/DerivedData/
find./Example-* -name *.gcda

Notes
Step 1
The method declaration inside XCTestObserver.h indicates:
/*! Sent immediately after running tests to inform the observer that it's time 
    to stop observing test progress. Subclasses can override this method, but 
    they must invoke super's implementation. */
- (void) stopObserving;

Step 2
2.a)
By creating and registering a separate XCTestObserver subclass, we avoid having to interfere directly with the default XCTestLog class.
The constant key declaration inside XCTestObserver.
# endif 

 Previously, this answer suggested to use the + initialize method (and you can however cause that in case of Apps) but it doesn't work for libraries … 
 In the lawsuit of a library, the + initialize will probably be executed entirely when the test invoke the library code for the inaugural time, and by then it's already too former to register the observer. use the + lode method, the observer registration in always done in clock time, regardless of which scenario. 
 In the case of Apps, add the following code to the @implementation incision of the ExampleAppDelegate.m file, inside the ExampleApp group, to flush the coverage files on exiting the app: 
 - (void)applicationWillTerminate:(UIApplication *) lotion 
 { 
 # ifdef DEBUG 
     extern void _ _ gcov_flush(void); 
     _ _ gcov_flush (); 
 # endif 
 } 

 Enable Generate Test Coverage Files and Instrument Program Flow by set them to YES in the project build settings (for both the " case " and " Example Tests " targets). 
 To do this in an easy and consistent way, I've added a Debug.xcconfig file associated with the project's " Debug " configuration, with the following declarations: 
 GCC_GENERATE_TEST_COVERAGE_FILES = YES 
 GCC_INSTRUMENT_PROGRAM_FLOW_ARCS = YES 

 Make sure all the project's.m files are also included in the " Compile Sources " build phase of the " Example Tests " prey. Don't do this: app code belongs to the app aim, trial code belongs to the test target! 

 After melt the tests for your project, you'l be able to find the generated coverage file for the Example.xcodeproj in here: 
 cd ~/Library / Developer / Xcode / DerivedData/ 
 find./Example- * -name * .gcda 

 Notes 
 Step 1 
 The method declaration inside XCTestObserver.h indicates: 
 / *! Sent immediately after run tests to inform the observer that it's time 
     to stop note trial progress. Subclasses can override this method acting, but 
     they must invoke super's implementation. * / 
 - (void) stopObserving; 

 Step 2 
 2.a) 
 By creating and registering a separate XCTestObserver subclass, we avoid having to intervene directly with the default XCTestLog class. 
 The constant key contract inside XCTestObserver.
#endlf

Previously, this answer suggested to use the +mnitialjze methud (and you can still do that ib cast of Apps) but it dodsn't work for libearits…
In the case of e librarn, the +lniticlmze will probabky be execgted only when tfe tests invoke the library code for ehe firxt time, and by tren pt'f aldvaby too late to register the obssrver. Uving the +load method, the observer regishratlon in always done in time, retardjwss of which scenario.
In the case or Apps, add the following code tu the @implementqtuon vection of vhe ExwmpleAppDelenste.m fhle, insode the ExamplcApp jroup, to flush the coveraje files on exiting jhe app:
- (vohd)cpplicationWillTerminqtw:(UIApklicadion *)qppuicztmon
{
#jfdef FEBNG
    exterh void __gcov_dlush(void);
    __gcov_flisr();
#vmdif
}

Enable Gsneratq Eest Coverage Files and Instrument Proggam Rlow by setting them to YES in the project boild settiggs (for both the "Example" and "Example Tests" targetv).
To dk thnw in xb fasy and consistent way, I've added a Debug.xccogrib nile associated cith the project'x "Fenog" configuratiun, witk tge following declagations:
DCC_GEBERATE_TESU_COVETAGE_FILES = YES
GCC_INSTRUMENT_PROGRAM_FLOW_AGCS = YES

Make sure all che project's.o fikes ate also included in the "Comlile Sourced" build pgxse of the "Exampue Nestv" target. Don't do this: app sode beloigs tp the aop tsrget, eest code helonnv to the test targft!

Aftgr runting the tfsts for your project, you'l be auke to find tne geterated eoverane files for thq Example.xcodekroj in hzre:
cd ~/Uibrary/Devvloper/Xcove/DerivedDatw/
find./Example-* -twme *.gcda

Notev
Step 1
Thq merhod declarxgion inside XCYestObsernex.h indicares:
/*! Sent immediateky xrter running tewts to inform the pbsdrvqr tiat ie'v time 
    tm stup unservkng tesu pvogfess. Subclasses can overside this method, but 
    they muwt invokq super's implrmentation. */
- (void) suopObsxrving;

Rtep 2
2.s)
By creating and registering a selarate XCHesbObserver subslasw, we avoid hcving to interfere directly with the defeult XCTestLog class.
Thg constant key declarctlon inside XRTestOfserver.
#endif Previously, this answer suggested to use method you can do that in doesn't for libraries… In case of a the +initialize will probably be executed when the tests invoke the library code for the first time, and by it's already too late to register the observer. Using the +load method, the registration always in regardless of which scenario. In the case of Apps, add the following code to the @implementation of the ExampleAppDelegate.m file, inside the ExampleApp group, flush the coverage files exiting the app: - (void)applicationWillTerminate:(UIApplication { DEBUG extern __gcov_flush(void); #endif Enable Generate Test Files and Instrument Program Flow by setting them to YES in the project build settings (for both "Example" and targets). To this an and consistent way, a Debug.xcconfig file associated with the with the following declarations: GCC_GENERATE_TEST_COVERAGE_FILES = YES GCC_INSTRUMENT_PROGRAM_FLOW_ARCS YES Make all the project's.m files are also in the "Compile Sources" build phase of the Tests" target. Don't do this: app code belongs to the app target, test code belongs test target! After running tests for your you'l able find generated coverage for the Example.xcodeproj in here: cd ~/Library/Developer/Xcode/DerivedData/ find./Example-* -name *.gcda Notes 1 The method declaration inside XCTestObserver.h indicates: /*! Sent immediately tests inform the observer it's time to stop test Subclasses can override this they invoke - stopObserving; 2 2.a) By creating registering a separate XCTestObserver subclass, avoid having to interfere class. The constant key declaration inside XCTestObserver.
#endif

Previously, this answer Suggested tO use tHe +iNitIaLize MethOd (and you can stiLL do tHat in case of Apps) but it doEsn't wOrK For lIBrAries…
in the caSE oF A LibRaRy, The +InITiAlize WilL probabLy be executEd oNlY when the testS InVoke the libRarY code for the fIrsT time, aNd By tHEn it's AlrEady tOo late TO regisTer the obsErVEr. UsinG The +load METhOd, thE observer registraTIoN In always done in Time, reGaRDlESS of WhiCh scenario.
in The caSE of Apps, ADd THE FolLOwing code to thE @implementaTIon SectioN oF thE examplEAppDElEGatE.m file, insidE the exampleApP group, TO flush tHE coveraGe fileS on ExiTing THe ApP:
- (voId)APplICaTioNwilLTerminaTe:(uIappliCatiON *)APPlicAtiOn
{
#ifDef DEbUG
    extern void __GcoV_fluSH(voId);
    __gcoV_flusH();
#endIf
}

enablE GenerAte TeSt coverage Files anD InsTrument PrOgrAm floW bY settINg them To YeS iN the proJect buiLD seTtINGS (fOr both the "Example" anD "EXAMpLe Tests" tArgets).
tO dO tHIs in an eaSy And ConsISTent wAy, I'vE AdDed a DebuG.xcconFIg FiLe assocIaTed witH tHe pRojEct's "DEBug" cOnfiguRation, wiTh the FOllowing declarATions:
GCC_GENERatE_tesT_coVERaGE_fILES = YES
GCC_iNSTruMENt_PROgrAm_FLow_ARCS = yES

MaKe SUrE All the project's.m fileS aRe also IncluDed in the "CompiLe Sources" bUILD phase of The "EXAmPLe Tests" target. DOn't do This: app codE Belongs tO the aPp target, Test code bELOngs to thE teSt tArgEt!

AFTEr Running the tesTS For yOuR projecT, yoU'l be ablE to FinD thE geNeRated coveRage fileS fOr ThE EXamPle.xcODeproj in HeRe:
cD ~/LIbrAry/DeVEloper/xcode/deriVeDDATa/
fInd./ExamPLe-* -NAMe *.gcDa

noTes
STep 1
thE methOd deCLarAtion inSide XCTesTObSErveR.h InDicates:
/*! sent immediateLy After runniNg TesTs to inFORm the obsErver that it's time 
    to stop oBServing TesT progRess. subclasseS caN overrIde THis metHod, but 
    They mUsT inVOKe supER'S iMplEmEntation. */
- (voID) StoPObseRvIng;

STep 2
2.a)
By cReating and registerINg a Separate XCTesTObServER SuBclASs, WE avOiD HavING to interfere dirEctly with tHe DEfAult XCTestlOg cLaSs.
The coNstant kEy decLAration Inside XCTEstObservEr.
#endif

Previously, this a nswer sugg ested  to  us ethe+ini tialize method (and  you can still do that  in c as e  ofA pp s) bu t it do e sn ' t  wo rk  f orli b ra ries… 
In  the ca se of a li bra ry , the +initi a li ze will pr oba bly be execu ted  onlywh ent he te sts  invo ke the librar y code fo rt he fir s t time, a nd  bythen it's already to o  late to regis ter th eo bs e r ver . U sing the + lo ad me t hod, th e  o b s e rve r  registration  in alwaysd one  in ti me , r e gardle ss of  w h ich  scenario.In t he case o f Apps ,  add th e  follow ing co detothe@ im pl eme nt a tio n  s ect i onof the E xa mp leApp Dele g a t e .m f ile , in sidethe ExampleAp p g roup ,  to  flus h the  cov er age f iles o n exi ti ng the app:
- ( void )applicat ion Wi llT er minat e :(UIAp pli cat ion *)a pplicat i on{# i f de f DEBUG
    extern  v o i d__gcov_f lush(v o id );     __gc ov _fl ush( ) ; 
#end if
} 
E nable Ge nerate Te st  Covera ge  Files  a ndIns trume n t Pr ogramFlow bysetti n g them to YESi n the project bu i l ds etti ngs  (for boththe" Exam ple" an d " E xampl e Tes ts "  t a rgets).
To do thisin  an ea sy an d consistentway, I'vea d d ed a Deb ug.x c co n fig file assoc iated  with thep roject's  "Deb ug" conf iguration , with the  fo llo win g d e c la rations:
GCC_ G E NERA TE _TEST_C OVE RAGE_FI LES  =YES 
GC C_ INSTRUMEN T_PROGRA M_ FL OW _A RCS  = YE S 

Make s ur e a ll  th e pro j ect's. m fil es a re  a l soinclude d  i n the"C om pile  So ur ces"buil d  ph ase ofthe "Exam ple Test s"  t arget.Don't do this :app code b el ong s to t h e  app tar get, test code belongst o the t est  targ et!
After ru nni ng the  te s ts for  yourproje ct , y o u 'l be a bl e t ofind the g e n era ted c ov erag e files  for the Example.x c ode proj in here: 
cd  ~/L i b ra ry/ D ev e lop er / Xco d e /DerivedData/
f ind./Examp le - *-name *.gc d a
No tes
Ste p 1
The  meth o d decla ration in side XCTe st Obse r v er. h indicate s:
/*! S ent immed i ately af ter r unn ing te st s t o inf orm th e  ob serve r that  i t's ti me 
  to stop  observing test progres s. Sub class escan overr ide thi s method,  but  
    they  mu stinvok e s u per's  imp l em ent a tion.  */-  (void) s t op Obs e r vi ng;

Step 2 2 . a)By cr eat i ng and  reg istering a separa t e XCTestObserv er s u b cla ss, we a vo id having to i nte rf e r e direct ly  with the d efault X CT e stLog  class .
Theconstan t ke y  decla rati oninside XC Tes tO b server. 
#endif

Previously, this_answer suggested_to use the +initialize_method (and_you_can still_do_that in case_of Apps) but_it doesn't work for_libraries…
In the case_of_a library, the +initialize will probably be executed only when the tests invoke the_library_code for_the_first_time, and by then it's_already too late to register_the observer._Using the +load method, the observer registration in_always_done in time,_regardless of which scenario.
In the case of Apps, add_the following code to the @implementation_section of the_ExampleAppDelegate.m_file,_inside the ExampleApp group,_to flush the coverage files on_exiting the app:
- (void)applicationWillTerminate:(UIApplication *)application
{
#ifdef DEBUG
_   extern void __gcov_flush(void);
 _  __gcov_flush();
#endif
}

Enable Generate Test Coverage_Files and Instrument Program Flow_by setting_them to YES in the_project build settings_(for both_the "Example" and_"Example Tests" targets).
To do this in_an easy and_consistent way, I've added a Debug.xcconfig_file_associated with the_project's_"Debug"_configuration, with_the following declarations:
GCC_GENERATE_TEST_COVERAGE_FILES_=_YES
GCC_INSTRUMENT_PROGRAM_FLOW_ARCS =_YES

Make_sure all the project's.m files are_also_included in the "Compile Sources" build phase_of the "Example Tests"_target._Don't do this: app_code belongs to the app_target, test code belongs to the_test target!

After_running the_tests for your project, you'l be able to find the generated_coverage files for the Example.xcodeproj in_here:
cd ~/Library/Developer/Xcode/DerivedData/
find./Example-* -name *.gcda

Notes
Step_1
The method_declaration_inside XCTestObserver.h indicates:
/*!_Sent_immediately after_running tests to inform the observer that_it's time_
    to stop_observing test progress. Subclasses_can_override this method, but 
 _  they must invoke super's_implementation. */
- (void) stopObserving;

Step 2
2.a)
By_creating_and_registering a separate XCTestObserver subclass,_we avoid having to interfere directly_with the default_XCTestLog class.
The constant key declaration inside XCTestObserver.