How to Send Data from UITest App Target to App Target

We are swizzling URLProtocol to custom URLProtocolMock and sending custom response to the API requests.
The URLProtocolMock has a custom Dict [String? : Data] which takes URL.abosulte string and its associated Data.
On startLoading() function, we compare the request,absolute string with the customDict, if the value is found we send the assoicated Data in the response.
So when we update the dictionary from UITestApp, and when the start loading is called in the App, the dictionary is always nil. Seems like UITestApp has its own instance of URLProtocolMock and App has its own instance of URLProtocolMock.
Is there way to share data from UITestApp and App seemlessly?
using environmental variables, makes to us to relaunch the app everytime and its not seemless.

Replies

To be clear:

  • Unit tests run inside your app’s process, and thus can set up mocks and so on.

  • UI tests run in a separate process, and thus setting up mocks is pointless because it only affects the process running the UI test.

If you’re relying heavily on mocks, it sounds like you should be creating a unit test rather than a UI test.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Yes i completey agree on that.
When/What to UITests and when/what to Unit Test is always the right question to ask.


But there are instances where we do have to provide mock data.
Is there any better way than using enviromental variables?

Is there any better way than using enviromental variables?

Given that the focus of UI tests tends to be end-to-end testing, I generally recommend that you set up a test server and have it vend the test responses you need. You will, of course, need an environment variable (or command-line argument) to tell your app to talk to that server.

There’s other possibilities here [1], but they’re not directly supported by XCTest so you’d have to build a bunch of infrastructure yourself. And that brings us back to the UI test versus unit test debate. Is it better to build that infrastructure to support your UI tests? Or use that time to a) move more code to unit tests, and b) set up a test server for your UI tests to do end-to-end testing? You’ll have to make that decision for yourself, but I know which one I’d do (-:

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] For example, you could use an environment variable to enable your

NSURLProtocol
subclass in app being tested and have that subclass use some sort of IPC mechanism to get the results it needs from your UI test process.

Thank you. Those suggestions helps a lot.