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.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

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.

Target membership

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.

Tests Running

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!