访问从xcodebuild命令行传入的用户定义变量
我正在使用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"];
访问它们[[NSUserDefaults standardUserDefaults] valueForKey:@"HOST_URL"];
xcodebuild -project CalculatorTestClient.xcodeproj -scheme CalculatorTestClient -destination '%s' ACCOUNT_ID=%s HOST_URL=%s test"
这两种方法都不适用于我。 从命令行传递用户定义变量的最简单方法是什么?
与@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
到目前为止,我只能使这种方法起作用:
$ 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"];
我为我的案例做的是使用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)"