【问题标题】:Accessing user defined variables passed in from xcodebuild command line访问从 xcodebuild 命令行传入的用户定义变量
【发布时间】:2014-06-10 16:17:16
【问题描述】:

我正在使用 xcodebuild 运行我的 xctests,需要传入一些 environment variables。 在下面的示例中ACCOUNT_ID 和HOST_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


【解决方案1】:

与@Paul Young 类似,我能够通过对方案进行一些修改来使其工作。这是我的解决方案:

对于 Xcode 中的方案(Xcode > 你的方案 > 编辑方案 > 测试 > 参数选项卡 > 环境变量):

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

在代码中(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

【讨论】:

  • 这是否也仅适用于 OSX?我希望有一个适用于 iOS 的解决方案。
  • 这行得通。这应该被接受答案@wherewouldibe
  • 我无法让它适用于 iOS。最终只是将 "$(ACCOUNT_ID) 放入值而不是传入的值
  • @dadougster 我遇到了同样的问题,因为我正在运行 test-without-building 但是如果你先构建然后运行测试 - 它可以工作
  • @dadougster 当取消选中“使用运行操作的参数和环境变量”以及将下拉“扩展变量基于”更改为 UITest 目标时,我可以使其工作以获取环境变量在测试方案中。
【解决方案2】:

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

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

/usr/libexec/PlistBuddy -c "add :APPNAME-TARGETNAME:EnvironmentVariables:KEYNAME string 'VALUE'" "(Path to XCTestRun file)"

【讨论】:

【解决方案3】:

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

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

【讨论】:

  • 刚刚注意到这仅适用于我的 OS X,不适用于 iOS。
  • 是的,我的评论指出,它仅在针对 OS X 而非 iOS 时对我有效。
猜你喜欢
  • 1970-01-01
  • 2014-08-08
  • 1970-01-01
  • 2013-05-07
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多