Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xctest - preprocessor macro [duplicate]

Tags:

xcode

xctest

Is it possible to have a macro defined in (testing) target settings or in the testing .pch file such that it's passed to the entire app?
Or is there any macro already available to check (from code) if we're running a test?

e.g.:

#if TEST=1
  // do something
#else
  // do something else
#endif

The reason that I want this is to skip some code, asserts etc. during testing (without having to change a #define in the main app .pch each time I run tests).

Thanks.

like image 771
alex-i Avatar asked Jun 18 '14 10:06

alex-i


1 Answers

It looks like you can do this in a very similar way to Objective-C. The swift compiler takes the -D command switch. To adapt this to testing I defined the Literal I wanted only in the Test Target Build settings.

Instructions:

Build settings for the Test Target -> 
      Swift Compiler Custom Flags -> 
         -DTEST (yes, including the -D prefix)

Enables this code:

// Objective-C and Swift
#if TEST
// Test only code version code
#else
// App only code
#endif

I found the solution at this article on transitioning to swift.

like image 140
ahalls Avatar answered Oct 17 '22 09:10

ahalls