6

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.

alex-i
  • 5,406
  • 2
  • 36
  • 56
  • 1
    Have you gotten any further with this issue. I would like to add some test fixtures to my code. But don't want it to show up in the production code. Some extensions and helper functions. That kind of thing? – ahalls Aug 20 '14 at 04:52
  • I personally ended up using something similar as described here: http://stackoverflow.com/a/15725328/727817 . Another good option would be setting up a new build configuration just for testing http://stackoverflow.com/a/14718914/727817 – alex-i May 20 '16 at 05:57

1 Answers1

4

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.

ahalls
  • 1,095
  • 1
  • 12
  • 23
  • 1
    I believe this works the same as setting a `#define` inside the .pch of the test target, which only affects the test classes, but not the actual app classes. I've tested using `'Other C Flags'` though, maybe it's different for swift. – alex-i Apr 08 '15 at 08:49