Testing configurations

Sometimes as an iOS Engineer we face the problem of define behaviors or values for variables in a testing environment. For example, a different endpoint URL for the development and release builds, etc.

In this article I would cover the more simple way I found it to help me to achieve this goal.

Configurations

Let’s face the problem mentioned above, define different endpoints URL for each of the build (Debug, Release, etc). For this we need to create a different configuration in Xcode.

For this, you need to create a new build configuration by clicking on your Project Name in the File Navigator, then switch to the Info tab. Under Configurations click on the + icon and make a duplicate of your Debug build configuration and let’s name it Testing.

Next, go to the Build Settings tab and under Swift Compiler - Custom Flags add the -DTESTING flag to the Testing build configuration. Note that the flag needs to start with -D and followed by the name in uppercase.

Afterwards we need to edit the Scheme and under Test change the build configuration to Testing instead of the default Debug.

Now you should be able to use the TESTING preprocessor macro in your project like this:

#if TESTING
    endpointURL = "https://server1.com"
#else
    endpointURL = "https://server2.com"
#endif

Xcode 8: New Build Setting

Fortunately in XCode 8 Apple introduced a new Build Setting entitled SWIFT_ACTIVE_COMPILATION_CONDITIONS. Active Compilation Conditions is a new build setting for passing conditional compilation flags to the Swift compiler.

Before this new build setting we had to declare our conditional compilation flags under OTHER_SWIFT_FLAGS, remembering to prepends -D to the setting as we did before. Now we only need to pass the value TESTING to the new setting. We have new alternatives now 🙌.

Conclusion

So now you should have two different ways to define configurations for your Testing environment, I hope this can help you.

Thanks for reading! 🎉.

·