Skip to content
tezvyn:

Driving app state via launch arguments in XCUITest

Source: interviewHardHow cards are made

Summary

setting up test preconditions cheaply.

Key points

pass launchArguments and launchEnvironment, read them at startup to seed auth and flags, bypassing slow UI steps.

What's really being asked

The interviewer wants to see that you separate the scenario under test from the setup required to reach it, and that you know the launch-time channels XCUITest provides for injecting state.

The full answer

XCUIApplication exposes launchArguments, an array, and launchEnvironment, a dictionary, both set before calling launch(). The app inspects ProcessInfo at startup and branches accordingly: an argument like -isLoggedIn YES seeds a fake authenticated session, an environment value selects which feature flags are on, and another can point networking at a stub server or load fixture data. This means a test for the logged-in dashboard never touches the login screen. Centralize the wiring in a base XCTestCase or a builder so tests declare intent, for example launchLoggedIn(features: [.newCheckout]).

The mistakes people make

Automating the real login flow through taps in every test, which is slow, depends on the auth UI, and breaks dozens of unrelated tests when login changes. Hard-coding a backend so flags cannot vary. Leaving setup state to persist between tests, causing order dependence.

What usually comes next

How do you keep test-only branches out of the production binary or ensure they are inert in release? How do you reset state between tests? How would you stub the network alongside this state injection?

A concrete example

In the test: let app = XCUIApplication(); app.launchArguments += ["-uitest-loggedIn"]; app.launchEnvironment["FEATURE_NEW_CHECKOUT"] = "1"; app.launch(). In the app's startup code, guarded so it only activates under test, it checks ProcessInfo.processInfo.arguments.contains("-uitest-loggedIn") to install a stub session and reads the environment dictionary to flip the checkout flag. The dashboard test launches straight into an authenticated, feature-enabled state in milliseconds, with no login taps and no coupling to the sign-in screen.

Interview question

What is the main advantage of seeding login state through launchArguments instead of automating the login screen in each test?

  • a.Tests start in the needed state instantly and stay decoupled from the auth UICorrect
  • b.It removes the need to disable animations
  • c.It guarantees the tests can never become flaky
  • d.It increases the app's code coverage automatically
Why?

Injecting state at launch puts each test in its precondition instantly without re-driving the login UI, so unrelated tests no longer break when sign-in changes. It does not affect coverage, animations, or eliminate all flakiness.

Just read this? Test yourself on what you have been reading.

Read the original → repeato.app

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles