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

whe*_*ibe 13 xcodebuild ios xctest

我正在运行我xctests使用xcodebuild并需要传入一些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"

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

小智 30

与@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"]!
Run Code Online (Sandbox Code Playgroud)

在命令行上:

$ 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
Run Code Online (Sandbox Code Playgroud)

  • 我无法使它适用于iOS。最终仅将“ $(ACCOUNT_ID)放入值中,而不是传入的值中 (2认同)
  • @dadougster,当取消选中“使用运行操作的参数和环境变量”时,我可以使其能够获取环境变量,并将下拉列表“扩展变量基于”更改为测试方案中的 UITest 目标。 (2认同)

Mar*_*kri 7

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

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

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


Pau*_*ung 2

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

$ ACCOUNT_ID=foo HOST_URL=bar xcodebuild -project CalculatorTestClient.xcodeproj -scheme CalculatorTestClient clean test
Run Code Online (Sandbox Code Playgroud)

并通过以下方式访问它们:

NSDictionary *environment = [[NSProcessInfo processInfo] environment];
NSString *accountID = [environment objectForKey:@"ACCOUNT_ID"];
NSString *hostUrl = [environment objectForKey:@"HOST_URL"];
Run Code Online (Sandbox Code Playgroud)