Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode: preprocessor macros for conditional DEBUG / TEST code

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...

like image 251
MeXx Avatar asked Feb 21 '12 00:02

MeXx


1 Answers

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

like image 187
MeXx Avatar answered Oct 07 '22 17:10

MeXx