简体   繁体   English

如何从 XCUITest 代码访问 xcconfig 变量

[英]How to access xcconfig variables from XCUITest code

I need to access an environment variable named ENV that is set in the xcconfig file for my main app target, but from inside my UI test target.我需要访问一个名为 ENV 的环境变量,该变量在我的主应用程序目标的 xcconfig 文件中设置,但从我的 UI 测试目标内部。

After doing a ton of googling this is what I've tried:在进行了大量的谷歌搜索之后,这是我尝试过的:

  • set the main xcconfig as the configuration for the UITest target将主 xcconfig 设置为 UITest 目标的配置
  • add an environment variable ENV=$(ENV) in the test plan configuration在测试计划配置中添加环境变量ENV=$(ENV)
  • print the environment dictionary like this像这样打印环境字典
print(ProcessInfo.processInfo.environment)

But $(ENV) is not expanding to the real value.但是$(ENV)并没有扩展到实际价值。

I also tried adding a custom entry in the Info.plist for the UITest target我还尝试在 UITest 目标的Info.plist添加自定义条目

<key>ENV</key>
<string>$(ENV)</string>

And then accessing it via Bundle.main.infoDictionary but again I just get the string $(ENV) .然后通过Bundle.main.infoDictionary访问它,但我再次得到字符串$(ENV) Any idea what I am missing, or is this just impossible because of the way UI tests work in Xcode?知道我遗漏了什么,还是因为 UI 测试在 Xcode 中的工作方式而无法实现?

What you have to do is to either use the app target's xcconfig as the build config for your test target.您需要做的是使用应用程序目标的 xcconfig 作为测试目标的构建配置。 Or if you need to have specific test target's build config, you include the app's xcconfig into your test target xcconfig.或者,如果您需要特定测试目标的构建配置,您可以将应用程序的 xcconfig 包含到您的测试目标 xcconfig 中。

Then in your test plan settings you define your test plan environment var as ENV=$(ENV)然后在您的测试计划设置中,您将测试计划环境变量定义为 ENV=$(ENV)

Basically: apptarget.xcconfig: ENV=appenv基本上:apptarget.xcconfig: ENV=appenv

testtarget.xcconfig: #include apptarget.xcconfig testtarget.xcconfig: #include apptarget.xcconfig

in testPlan environment you define: ENV=$(ENV)在您定义的 testPlan 环境中: ENV=$(ENV)

XCConfig is used during build time. XCConfig 在构建期间使用。 The $(ENV) has to be expanded in build time to the value you want. $(ENV) 必须在构建时间扩展到您想要的值。 You will accomplish that by either using the apptarget.xcconfig as the xcconfig of your test arget or including it into your testarget.xcconfig您将通过使用 apptarget.xcconfig 作为测试目标的 xcconfig 或将其包含到您的 testarget.xcconfig 中来完成此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM