I have a Swift project, I added a unit test target to it. my config file Constants.swift contains a struct:
struct Constants {
struct Network {
#if APITESTS
static let serverURL = "https://www.servertest.com"
#else
static let serverURL = "https://www.server.com"
#endif
}
}
In my test target Other Swift Flags
I added -DAPITESTS
Then run the unit test with CMD+U
In a log present in my test, I can see that it's "https://www.server.com" that is selected here when it should be servertest.
If I use #if DEBUG instead of APITESTS, it works. If I try to add APITESTS within Active Compilation Conditions (just like DEBUG) it is still not recognized.
Any clear explaination?
To do that, goto Project (not target)→Build settings → Swift Compiler → Custom flags → Other swift flags section and edit the different configuration flags. Add the flags using the syntax “ -D <flag_name>”. NOTE: if you are not seeing Other swift flags section, you will be in the basic build settings mode.
Feature flags allow you to slowly rollout a feature, rather than doing a risky big bang launch, and are extremely helpful when used in a continuous integration and continuous delivery environment. At Optimizely, we commonly use feature flags to reduce the risk of complicated deploys like rolling out new APIs.
When you define a condition in the test target, it only affects the test; the main app is built based on its usual configuration. The two easy solutions I see here are to set an environment variable or define a special test configuration in the main app and set the scheme to use that configuration for tests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With