简体   繁体   中英

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.

After doing a ton of googling this is what I've tried:

  • set the main xcconfig as the configuration for the UITest target
  • add an environment variable ENV=$(ENV) in the test plan configuration
  • print the environment dictionary like this
print(ProcessInfo.processInfo.environment)

But $(ENV) is not expanding to the real value.

I also tried adding a custom entry in the Info.plist for the UITest target

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

And then accessing it via Bundle.main.infoDictionary but again I just get the string $(ENV) . Any idea what I am missing, or is this just impossible because of the way UI tests work in Xcode?

What you have to do is to either use the app target's xcconfig as the build config for your test target. Or if you need to have specific test target's build config, you include the app's xcconfig into your test target xcconfig.

Then in your test plan settings you define your test plan environment var as ENV=$(ENV)

Basically: apptarget.xcconfig: ENV=appenv

testtarget.xcconfig: #include apptarget.xcconfig

in testPlan environment you define: ENV=$(ENV)

XCConfig is used during build time. The $(ENV) has to be expanded in build time to the value you want. You will accomplish that by either using the apptarget.xcconfig as the xcconfig of your test arget or including it into your testarget.xcconfig

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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