views:

438

answers:

2

For the purposes of unit testing I'd like to create an iPhone project target in Xcode that includes all of the release application files, plus some additional files containing code useful for UI unit testing.

I can do this by duplicating the original application target; however, the problem with this is that every time I add a new source file to the app target, I need to also add it to the UnitTestUI target. It's not a big deal, just inconvenient to always remember to add files to both targets.

Is there some way to set up a dependency so that every file added to the original app target is also auto added the unit test target?

A: 

No there's not. Is there a particular reason you want every single file in your Unit Test target? This would include main.m and all classes that you aren't testing (such as maybe your view classes). In fact, if main.m is included in your Unit Test, then how would your Unit Test even run properly?

Kevin Ballard
I should clarify that this target is meant to test code that is dependent on having the whole app context available. (Such as some UIViewControllers.) As such it's more like a UI test framework, and is meant to cover things our other "headless" unit tests are unable to cover.
thrusty
Sure there is, targets can be direct dependencies of each other.
Jason Coco
+4  A: 

In Xcode, you can create targets which have direct dependencies on each other. There are a number of non-product building targets that can help with this in the Other category when adding a new target, depending on how simple or complicated your set-up is. Creating specific targets for running unit tests with a direct dependency on the main project target is very common and is documented by Apple and on a number of blogs.

In your situation, however, you may have to do a lot of tweaking on the new, UI testing target, but once it's set up, it will be very easy to maintain. Not knowing your exact situation, it's impossible to give you a step-by-step answer, but here are the general guidelines (tweak to suit your situation):

  1. Create the copy of your original target since most of your settings will be the same.
  2. Select your new target and open the inspector (⌘I)
  3. Under Direct Dependencies, click the + button and select your main target.
  4. Set up the new target as desired, with extra documentation/source/rules or whatever.

If you prefer dragging and dropping things around, you can also drag your original target (from under the Targets disclosure triangle) into your new target and it will automatically set up the dependency.

Now, select your testing target as the active target and it will always build with those rules. Also, if you add/change source in the main target, it will properly be rebuilt when building your testing target... no need to remember to add a source file to the testing target as well. I suggest taking some time to read the various Xcode docs and playing with a lot of the target templates available... in the long run, it really helps make using the product a lot more efficient. There are a lot of nifty things that can be done fairly easily in Xcode if you know how, even with very large or complex projects.

Jason Coco
Thanks a ton for this very helpful answer!
thrusty
Great answer. Originally I thought that I could delete my linked libraries from the new target as well, but nothing a few `git reset --hard`'s won't fix.
Yar