Adding Unit Tests to an existing iOS project with Xcode 4
WhenyoubuildanewiPhoneoriPadappfromscratch,youcangenerateanewprojectwithtestsusingXcode’sprojecttemplates,andthetestdependencyissetupcorrectly.
However,ifyouaddaunittesttargettoanexistingiOSproject,therearesomemanualstepsrequiredtosetupthetargetcorrectly.Followingthesestepswillgetyouupandrunningquickly:
- ProjectMyExistingApp->+AddTarget->iOS->Other->CocoaUnitTestingBundle
- Namethenewtargetsomethinglike“UnitTests”or“MyAppTests”
- Selectyournew“UnitTests”targetandclicktheBuildPhasestab,expandTarget4
- DependenciesandaddMyExistingAppasasaTargetDependency
- ClicktheBuildSettingstabandsettheBundleLoadersettingto$(BUILT_PRODUCTS_DIR)/MyExistingApp.app/MyExistingApp
- SettheTestHostbuildsettingto$(BUNDLE_LOADER)
- Gobacktoyourapptarget(notthetesttarget),settheSymbolsHiddenbyDefaultbuildsettingtoNO
Thatshouldtakecareofit,andyoucannowstartaddingtestsforyourapp.Withthisconfiguration,youcontinuetoaddapplicationclassesonlytoyourapptarget,andaddtestclassesonlytoyourtesttarget.Sincethetestsrunintheapprunninginthesimulator,thecodeundertestcansafelyinstantiateviewsandfonts,whichwouldotherwisecauseOCunittocrashwhenrunningwithouttheTestHostsetting.
refertohttp://twobitlabs.com/2011/06/adding-ocunit-to-an-existing-ios-project-with-xcode-4/