1

I need to disable the logging when running unit testing automatically so that the test speed can be significantly improved(~20s vs 0.374s, only a few tests right now, but a big loop in the testing code).

I tried to define a macro in the testing code header, like this:

#define UNIT_TESTING

And in the code to be tested, I first check if such macro is defined:

#ifndef UNIT_TESTING
NSLog("whatever log");
#endif

But it seems not working, I'm guessing if it's because I didn't import the testing code header in the code to be tested. I can't do this because then the log won't be printed even when not unit testing.

I'm relatively new to objective c and came from Java background, I guess there is something wrong with header/macro definition.

One more question is, do I ever need to define a macro for unit testing, isn't there anything already like that?

xu huanze
  • 269
  • 2
  • 9
  • Note that instead of testing `UNIT_TESTING` every time you want to log something, you can conditionally define a [variadic macro](http://stackoverflow.com/q/679979/) to `#define MYLOG(...) (NSLog(__VA_ARGS__))` or `#define MYLOG(...)`. Moreover, if you're using logging to print debugging information, you should instead use an interactive debugger; you'll get more control over the process and this issue just vanishes. – outis Jul 20 '12 at 06:19

1 Answers1

1

You need to define UNIT_TESTING in the build settings of your unit tests target.

Select your project in the left pane, then the unit tests target, then Build Settings --> All --> Preprocessor Macros.

TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76
  • Yes it works, thanks. And for absolute beginners, it might be worth to mention that you need to first select the All tab under the Build Settings page, so that the Preprocessor option will be visible. – xu huanze Jul 20 '12 at 07:03