访问从xcodebuild命令行传入的用户定义变量

问题描述 投票:10回答:3

我正在使用xcodebuild运行我的xctests并需要传入一些environment variables。在下面的例子中ACCOUNT_IDHOST_URL

我尝试将变量作为环境变量传递,并使用getenv ("ACCOUNT_ID") xcodebuild -project CalculatorTestClient.xcodeproj -scheme CalculatorTestClient -destination '%s' ACCOUNT_ID=%s HOST_URL=%s test"从测试中访问它们

并将它们作为user defaults传递并使用[[NSUserDefaults standardUserDefaults] valueForKey:@"HOST_URL"]; xcodebuild -project CalculatorTestClient.xcodeproj -scheme CalculatorTestClient -destination '%s' ACCOUNT_ID=%s HOST_URL=%s test"访问它们

这两种方法都不适用于我。从命令行传递用户定义变量的最简单方法是什么?

ios xcodebuild xctest
3个回答
24
投票

与@Paul Young类似,我可以通过对该方案进行一些修改来实现这一点。这是我的解决方案:

对于Xcode中的Scheme(Xcode> Your Scheme> Edit Scheme> Test> Arguments选项卡> Environment Variables):

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

在Code(Swift 3)中:

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

在命令行上:

$ 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

1
投票

到目前为止,我只能使用这种方法:

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

并通过以下方式访问

NSDictionary *environment = [[NSProcessInfo processInfo] environment];
NSString *accountID = [environment objectForKey:@"ACCOUNT_ID"];
NSString *hostUrl = [environment objectForKey:@"HOST_URL"];

1
投票

我为我的案例做的是使用xcodebuild build-for-testing命令并创建xctestrun文件,然后使用xcodebuild test-without-building运行测试。在这种情况下,您可以在运行测试之前更改其plist中包含环境变量的xctestrun文件。

因此,您需要使用PlistBuddy来更改plist环境键来运行脚本。例如,添加一个键:

/usr/libexec/PlistBuddy -c "add :APPNAME-TARGETNAME:EnvironmentVariables:KEYNAME string 'VALUE'" "(Path to XCTestRun file)"
© www.soinside.com 2019 - 2024. All rights reserved.