Varun Mehta
Varun Mehta

Reputation: 1753

How to do UI Testing for any ViewController in application in isolation

I have started doing UI Testing for my application. I have gone through the WWDC UI Testing tutorial and noticed that for all test i need to launch and setup my application before doing testing on particular ViewController which can be deep inside my application hierarchy.

I want to test every ViewController of my application in isolation like we do Unit Testing of class. Is this possible with UI Testing, introduced in Xcode 7, that i can render my ViewController with some mock data and test it.

Upvotes: 1

Views: 1154

Answers (1)

mokagio
mokagio

Reputation: 17491

Is this possible with UI Testing that i can render my ViewController with some mock data and test it.

This is not possible out of the box, but you could put in place some infrastructure in your application to achieve this.

You list two requirements: 1) access view controllers that are possibly deep in the navigation hierarchy, and 2) provide stub (stub is the correct term in this case) data to them.

XCUIApplication exposes the launchArguments and launchEnvironment properties to allow developers to pass extra configurations to the application from the UI tests, as the those cannot access the application code directly.

You could leverage those to pass configurations or commands to your app before starting in and have it configure itself into the state you want to test.

For example you could put in place a routing system in the app delegate and pass the route for the desired view controller in the UI tests. This way when the app start for testing it would read the route you gave it and load your view controller straightaway.

You could leverage the same technique to pass the data you need so that the view controller is initialized with it.

Unfortunately I'm just theorizing here, I have been playing around with this idea myself but haven't actually set out to implement it. I think putting in place such a setup and make it flexible enough would require a certain effort, as there are a lot of configuration to implement.

Upvotes: 2

Related Questions