How to access Test Plans launch arguments and environment in the app target during UI tests

I am able to reproduce this also in a minimal example.

Here is my setup:

  • a Hello World app and a UI test target.
  • a Test Plan which executes the UI test target
  • test plan configures application language as German for sanity testing whether changing the test configuration via the test plan in general works -> this works perfectly
  • test plan configures launch arguments and Environment variables in the shared settings "Arguments passed on launch"

  • In the ContentView of the app I try to access these values via ProcessInfo:
print(ProcessInfo.processInfo.arguments)
print(ProcessInfo.processInfo.environment)

Now I run the test plan Expected:

  • my custom launch arguments appear in arguments
  • my custom env vars appear in environment

Actual: none of them appear.

What am I doing wrong here?

Answered by Engineer in 794785022

The arguments and environment variables specified in an Xcode test plan are intentionally only provided to the test runner process — the process where you create and launch apps to be tested using XCUIApplication and similar APIs. If you wish to also make some, or all, of those arguments or environment variables available to an app being tested, you can read the values using ProcessInfo.processInfo (as you showed above), and assign the XCUIApplication.launchArguments or XCUIApplication.launchEnvironment property as needed prior to calling XCUIApplication.launch(...).

Arguments and environment variables are not automatically forwarded to app processes because in some situations, the two processes may need different values for them, so being explicit about these property assignments helps support more advanced workflows.

Also on the XCUIApplication object those are empty

Accepted Answer

The arguments and environment variables specified in an Xcode test plan are intentionally only provided to the test runner process — the process where you create and launch apps to be tested using XCUIApplication and similar APIs. If you wish to also make some, or all, of those arguments or environment variables available to an app being tested, you can read the values using ProcessInfo.processInfo (as you showed above), and assign the XCUIApplication.launchArguments or XCUIApplication.launchEnvironment property as needed prior to calling XCUIApplication.launch(...).

Arguments and environment variables are not automatically forwarded to app processes because in some situations, the two processes may need different values for them, so being explicit about these property assignments helps support more advanced workflows.

How to access Test Plans launch arguments and environment in the app target during UI tests
 
 
Q