11

I have sections in my code (for example in the AppDelegate.m) that should not be compiled for the Unit Tests, like

#ifndef CONFIGURATION_TESTS
// Code that should not be compiled in the Unit Tests
#endif

Targets are set up by Xcode when you select 'add Unit Tests' when creating a new project. In the Project File, I have added the Flag CONFIGURATION_TESTS to the Preprocessor Macros for the MyAppTests Built-Target but not for the MyApp Target.

This was the suggested way in many posts that i've found.

But this doesn't work, because (i guess) the MyAppTests target has the MyApp target as a dependency and because AppDelegate.m is added to the MyApp target, it gets compiled with the MyApp build settings and hence, the CONFIGURATION_TESTS is not defined.

In the unit-test files the macro is defined and behaves as expected (because the test files are only build by the MyAppTests target ?)

Does anyone know how do this, i thought this would be a common problem...

2
  • 1
    I have the same issue and can't figure it out Commented May 1, 2012 at 21:06
  • "This doesn't work: 1) CONFIGURATION_TEST is always true? 2) never true? 3) works in unit test but not in app? 4) works in app but not unit tests? (inquiring minds want to know…) Commented Jan 1, 2013 at 18:20

2 Answers 2

13

I've finally found the answer myself: The key is to create a new build configuration (lets call this configuration Testing)

Then you configure the values of the preprocessor macro accordingly, so for configuration Debug and Release you set CONFIGURATION_TESTS=0 and for configuration Testing, you set CONFIGURATION_TESTS=1

Finally you edit the Unit-Test schema to use Testing as build configuration. That's it :P

I've written a detailed step-by-step explanation over at my blog

Sign up to request clarification or add additional context in comments.

1 Comment

Dumb question: Why do you need CONFIGURATION_TESTS=0 for non-test build configurations? I left this part out and everything seemed to work fine.
3

You need to set "CONFIGURATION_TESTS=1", not just "CONFIGURATION_TESTS"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.