Accessing user defined variables passed in from xcodebuild command line

wherewouldibe picture wherewouldibe · Apr 25, 2014 · Viewed 7.9k times · Source

I am running my xctests using xcodebuild and need to pass in some environment variables. In the example below ACCOUNT_ID and HOST_URL.

I tried passing in the variables as both environment variable and accessing them from the test using getenv ("ACCOUNT_ID") xcodebuild -project CalculatorTestClient.xcodeproj -scheme CalculatorTestClient -destination '%s' ACCOUNT_ID=%s HOST_URL=%s test"

And passing them in as user defaults and accessing them using [[NSUserDefaults standardUserDefaults] valueForKey:@"HOST_URL"]; xcodebuild -project CalculatorTestClient.xcodeproj -scheme CalculatorTestClient -destination '%s' ACCOUNT_ID=%s HOST_URL=%s test"

Neither approach worked for me. What is easiest way to pass user defined variables from commandline?

Answer

atgrubb picture atgrubb · Feb 27, 2017

Similar to @Paul Young I was able to get this to work, with a couple of modifications to the Scheme. Here's my solution:

For the Scheme in Xcode (Xcode > Your Scheme > Edit Scheme > Test > Arguments tab > Environment Variables):

Name Value ACCOUNT_ID $(ACCOUNT_ID) HOST_URL $(HOST_URL)

In Code (Swift 3):

let accountID = ProcessInfo.processInfo.environment["ACCOUNT_ID"]!
let hostURL = ProcessInfo.processInfo.environment["HOST_URL"]!

On the command line:

$ xcodebuild -project YourProject.xcodeproj \
-scheme "Your Scheme" \
-sdk iphonesimulator \
-destination 'platform=iOS Simulator,name=iPhone 7,OS=10.2' \
-derivedDataPath './output' \
ACCOUNT_ID='An Account ID' \
HOST_URL='www.hosturl.com' \
test