Local API Call Tutorial with WireMock and UI Tests in Xcode
Learn how to use WireMock, a tool you can use in conjunction with User Interface tests to provide a local copy of remote API call results. By Mark Struzinski.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Local API Call Tutorial with WireMock and UI Tests in Xcode
20 mins
- Getting Started
- Running the Tests
- UI Test Code
- Challenges With the Current Setup
- Mitigating Network Failures
- Overview of WireMock
- Setting Up WireMock
- Setting Up a Mock Response
- Setting Up Mappings
- Verify Functionality
- Integrating WireMock
- Allowing Loads from a Local HTTP Server
- Defining a Launch Argument
- Editing the Xcode Scheme
- Updating to Use Launch Arguments
- Adding a Utility Function
- Checking for Local flag in Network Client
- Perform a Run with WireMock
- Testing Against WireMock
- UI Test Launch Arguments
- Where to Go From Here?
Testing Against WireMock
Now, perform the final steps to get your UI tests set up with the right launch arguments.
Back in Xcode, expand the StarWarsInfoUITests group in the Project navigator. Then Control-click on the Utility group. Create a new file named LaunchArguments.swift. Make sure to check **StarWarsInfoUITests** and remove the check from **StarWarsInfo** in the **Targets** section of the **Save As** dialog.
Add the following code:
struct LaunchArguments {
static var launchLocalArguments: [String] {
return [
"-runlocal"
]
}
}
Finally, expand the Tests group in the project navigator and open StarWarsInfoUITests.swift. In each of the two tests, right after the creation of the local app
parameter, add the following line:
app.launchArguments = LaunchArguments.launchLocalArguments
This adds your -runlocal launch argument to each test.
Now you can test the UI test running against WireMock instead of the live network.
Switch back to Xcode and run your UI tests by pressing Command-U. If you watch Terminal while the tests run, you’ll see output from WireMock as your tests request data and they’re returned from the local server.
Congratulations! You’ve eliminated the network as a dependency for your UI tests.
Where to Go From Here?
In addition to mocking the same responses you always get from live network requests, you can now send back different scenarios to your UI and see how the app responds.
For example:
- Does your app show proper error messaging if it receives an HTTP 500 status?
- What happens to your UI if it receives an empty data set?
- Does the app respond in the right way if it receives a malformed data set?
- What happens if some of the strings received are too long for the UI? Does everything wrap or truncate as expected?
Try adjusting your mocked responses to test different flows inside your apps. WireMock also allows you to vary responses by many different variables for the same endpoint, such as query parameters and headers. Check out the documentation to see what else is possible.
Good luck, and please leave any questions in the comment section!