whe*_*ibe 13 xcodebuild ios xctest
我正在运行我xctests使用xcodebuild并需要传入一些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"
这两种方法都不适用于我.从命令行传递用户定义变量的最简单方法是什么?
小智 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)
我针对我的案例所做的是使用命令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)
到目前为止,我只能使这种方法发挥作用:
$ 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)
| 归档时间: |
|
| 查看次数: |
5349 次 |
| 最近记录: |