5

I'm using Unit Testing in XCode 4. I've been adding my application *.m files to the unit test target "compile sources" as I refer to them so the unit test will build/run.

Is there a better way here?

Note - I've just tried adding the application to the unit test target "target dependencies", but this didn't seem to work (still get some build errors).

Greg
  • 34,042
  • 79
  • 253
  • 454

3 Answers3

8

Prior to Xcode 4, the approach you describe was necessary: An application test target had to essentially be a copy of your actual application, plus tests.

But with Xcode 4, we no longer need to do this for testing in the simulator. Instead, use a target that is a unit test bundle, and add your tests to the bundle. During testing, the application is launched, and the test bundle will be injected into and linked with the application, all at runtime.

Since it sounds like you tried this approach, let's discuss the actual errors you get.

Jon Reid
  • 20,545
  • 2
  • 64
  • 95
  • Hi Jon - perhaps I'm running the tests the wrong way - what I have been doing is changing the profile to UnitTests and then hitting Command-U (test). In this case it appears my Unit Test target requires the supporting files. Was this what you'd understood re my problem? – Greg May 03 '11 at 22:51
  • To see how it's done in a more integrated way, create a brand new project. Be sure to select "Include Unit Tests". You'll get a single scheme that incorporates two targets: the main target and the test target. Study the Build Settings of the test target. Also study the scheme settings. – Jon Reid May 03 '11 at 23:13
  • As it's a long way down to check all the build settings, I just want to say that I only had to change/add stuff in "Linking" => BundleLoader and "Unit Testing" => Test Host, to get it working. Hope this could help someone to not have to check each setting there. PS: I think this should be the accepted answer! – d4Rk Oct 24 '13 at 09:20
  • It's true that everything builds and runs correctly, but autocomplete doesn't work when you do this. How do you fix that? – teradyl Jul 13 '15 at 21:49
  • Something's still wrong if your autocorrect isn't working. – Jon Reid Jul 13 '15 at 23:52
2

Yes, you will need to add each and every .m file you need to your unit test target.

This will include them in the target compile and link phases.

Claus Broch
  • 9,212
  • 2
  • 29
  • 38
0

if testTarget was created based om SWIFT language in Obj-C project

It would be enough to make scheme of testTrget but such away: product -> scheme -> edit scheme -> Build tab: checked only Test nothing more.

IMPORTANT: You should not include each .m file into test target Just to add headers into testTarget-Bridging-Header.h

Note: if you will CHECK more in scheme or INCLUDE .m file into testTarget you can get headers conflicts

Svitlana
  • 2,938
  • 1
  • 29
  • 38