24

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?

ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48
wherewouldibe
  • 251
  • 1
  • 2
  • 5
  • It seems not to be easy. Have a look here: http://blog.manbolo.com/2013/05/17/passing-user-variable-to-xcodebuild – Opal Apr 26 '14 at 17:13

3 Answers3

56

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

For iOS, make sure to do modify the scheme with the following:

  1. Uncheck the Use the Run action's arguments and environment variables
  2. Change the drop down "Expand Variables Based On" to the Test target in the Test scheme.
mokagio
  • 16,391
  • 3
  • 51
  • 58
atgrubb
  • 1,145
  • 7
  • 11
  • Does this work for OSX only too? I would appreciate a working solution for iOS. – cakl Mar 29 '17 at 21:13
  • My solution is for iOS. This is how I pass in variables to my iOS UI tests – atgrubb Mar 30 '17 at 22:41
  • I have so much love for you right now. It took me a while to find this. Thanks! Exactly what I needed. – helkarli Apr 11 '17 at 15:52
  • 1
    this works. this should be accepted answer @wherewouldibe – Hasaan Ali Aug 29 '18 at 13:12
  • 15
    I have not been able to get this to work for iOS. Is ends up just putting "$(ACCOUNT_ID) into the value instead of the passed in value – dadougster May 29 '19 at 15:46
  • 1
    @dadougster I had the same issue because I was running test-without-building but if you build first then run the test - it works – Dimu4 Mar 06 '20 at 22:13
  • might help to specifically call out that you need to set the environment variable for the Scheme > Test .. not Scheme > Run (which is displayed by default) .. then it will be picked up. – timzilla Oct 03 '20 at 18:41
  • 4
    @dadougster I could make it work to get the environment variable when uncheck the "Use the Run action's arguments and environment variables" as well ad change the drop down "Expand Variables Based On" to the UITest target in the Test scheme. – Bruno Bieri Dec 22 '20 at 15:47
  • @BrunoBieri 's comment make sense, this answer should be accepted. – alwaysday1 Aug 02 '21 at 07:10
  • I had to use a different name when specifying the var in the xcode scheme: `ACCOUNT_ID` = `$(ACCNT_ID)`. Not happy about it, though – huwr Feb 23 '23 at 05:51
7

What I did for my case is I used the xcodebuild build-for-testing command and create the xctestrun file then using xcodebuild test-without-building to run the test . in this case you can change the xctestrun file which has the environment variables in its plist before running your test .

so you need to run a script by using PlistBuddy to change your plist environment keys . for example to add a key :

/usr/libexec/PlistBuddy -c "add :APPNAME-TARGETNAME:EnvironmentVariables:KEYNAME string 'VALUE'" "(Path to XCTestRun file)"
Maryam Fekri
  • 605
  • 8
  • 19
2

So far I've only been able to get this approach to work:

$ ACCOUNT_ID=foo HOST_URL=bar xcodebuild -project CalculatorTestClient.xcodeproj -scheme CalculatorTestClient clean test

and accessed them via:

NSDictionary *environment = [[NSProcessInfo processInfo] environment];
NSString *accountID = [environment objectForKey:@"ACCOUNT_ID"];
NSString *hostUrl = [environment objectForKey:@"HOST_URL"];
Paul Young
  • 1,489
  • 1
  • 15
  • 34