Reading environment variables from iOS and macOS unit tests
·
Igor Kulman
In some environments like the CI/CD you might need to read environments variables from your iOS or macOS unit tests.
A typical example might be reading secrets or configuration that is CI/CD specific and you do not want to store it in source control.
In Swift you can read environment variables using the ProcessInfo.processInfo.environment
dictionary, for example
let host = ProcessInfo.processInfo.environment["apiHost"] ?? "default fallback host"
The problem is this code does not just work, there is one other step you need to do first.
You need to edit your Test scheme and add every environment variable you want to read to the Environment Variables
section.
You can rename the variables but for simplicity and readability you should probably keep the same names.
Enjoyed this article? Support my work by buying me a coffee! ☕️
Buy Me a CoffeeSee also
- Migrating internal iOS libraries to a monorepo using Swift Package Manager
- Visual Studio Code setup for iOS development with Copilot
- Drawing custom areas into map snapshots in iOS
- Use OpenVPN as a proxy to more comfortably access internal Gitlab
- Using reordering and selection at the same time in UITableView