Accessing user defined variables passed in from xcodebuild command line

China☆狼群 提交于 2019-11-30 17:53:25

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

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"];

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)"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!