Mocking External Services in Appium Tests for Improved Reliability and Speed

When it comes to testing mobile applications using Appium, we often focus on end-to-end tests. These tests exercise the app from the user interface down to the backend services. While such testing is valuable, as it mimics real user interactions, there are instances where the reliability of external services becomes a hindrance. This article explores the benefits and implementation of mocking external services in Appium tests to ensure that our app behaves correctly regardless of backend service reliability.

Lana Begunova
Women in Technology
5 min readMay 20, 2024

--

Why Mock External Services?

🤯 Real-World Challenges

In real-world scenarios, the internet is not always reliable. Requests made by an app to external services can fail for numerous reasons unrelated to the app itself. These can include network issues, server downtimes, or even load problems on the backend service during testing. These failures can lead to flaky tests, which are tests that pass or fail intermittently without any code changes.

🛠️ The Solution: Mock Servers

To mitigate these issues, we can use a mock server to simulate the backend service. A mock server intercepts HTTP requests from the app and responds with predefined responses. This method ensures that the app receives consistent and predictable data, making tests faster and more reliable.

Benefits of Using a Mock Server

🅿🆁🅾🆂

💨 𝑺𝒑𝒆𝒆𝒅
Mock servers can reside on the same network as your testing devices, making API calls significantly faster than if they had to traverse the internet.

🔋 𝑹𝒆𝒍𝒊𝒂𝒃𝒊𝒍𝒊𝒕𝒚
Since a mock server is a simple piece of code that responds with canned responses, it is inherently more reliable than a real backend service. Removing the internet from the equation further reduces potential points of failure.

💪 𝑭𝒍𝒆𝒙𝒊𝒃𝒊𝒍𝒊𝒕𝒚
Mock servers allow for greater flexibility in development and testing. They enable testing of the app before or during the development of the backend service, fostering independence between frontend and backend teams.

Downsides of Mock Servers

🅲🅾🅽🆂

⚙️ 𝑴𝒂𝒊𝒏𝒕𝒆𝒏𝒂𝒏𝒄𝒆
A mock server and its canned responses need to be maintained separately from the actual backend service. This can increase the workload for developers.

⛵️ 𝑨𝑷𝑰 𝑫𝒓𝒊𝒇𝒕
There’s a risk that the mock server might not stay in sync with the actual backend service. To prevent this, the team responsible for the backend should also maintain the mock server.

😓 𝑪𝒐𝒎𝒑𝒍𝒆𝒙𝒊𝒕𝒚
Implementing a mock server requires additional logic in the app to switch between real and mock services. Tests also need to manage mock server expectations and ensure proper coordination.

Implementing a Mock Server Strategy

Here’s a step-by-step guide to implementing a mock server in your Appium tests:

1. Choose a Mock Server Package

Select a mock server package that suits your needs. For Java and JavaScript, the aptly named Mock Server is a popular choice.

2. Modify App Code

Update your app to connect to the mock server instead of the real API server. This can be done by:
• Hardcoding the mock server’s host and port in a separate build for testing.
• Passing startup arguments to specify the API endpoints.
• Adding a UI element in the app to set server details manually.

3. Start and Stop the Mock Server

Ensure you can start and stop the mock server before and after your tests. This setup guarantees that the server runs on the specified host and port.

4. Configure Mock Server Expectations

Set up the mock server with predefined responses for expected API calls. If you’re using a mock server package, it will likely have its own way of specifying these behaviors.

5. Coordinate with the App

Make sure your app can communicate with the mock server. Both should be on the same network to avoid unnecessary delays.

🧐 Example Implementation

Putting all of these pieces together, the actual test flow would look like this (in pseudocode):

Before:
• Start mock server at certain port
• Start Appium session with app (connected to server)

Test:
• Set mock server expectations
• Use Appium to drive app, which makes calls to mock server under hood
• Verify app behaves properly given mock server responses

After:
• Quit Appium session
• Shut down mock server

Here’s an example using MockServer Client to mock a login request:

In this setup, the mock server will respond with a success message whenever it receives a login request with the specified credentials.
new MockServerClient("localhost", 1080)
.when(
request()
.withMethod("POST")
.withPath("/login")
.withBody("{\"username\": \"foo\", \"password\": \"bar\"}")
)
.respond(
response()
.withBody("{"\status\": \"success\"}")
);

🌊 Test Flow

Your test flow might look like this.

🌀𝑩𝒆𝒇𝒐𝒓𝒆 𝑻𝒆𝒔𝒕
• Start the mock server on a specified port.
• Initiate the Appium session with the app connected to the mock server.

🌀𝑫𝒖𝒓𝒊𝒏𝒈 𝑻𝒆𝒔𝒕
• Set expectations on the mock server.
• Use Appium to interact with the app, which makes API calls to the mock server.
• Verify the app’s behavior based on the mock server’s responses.

🌀𝑨𝒇𝒕𝒆𝒓 𝑻𝒆𝒔𝒕
• End the Appium session.
• Shut down the mock server.

Addressing Cloud Testing Challenges

While running mock servers locally is ideal, cloud-based testing environments can introduce latency. Despite this, mock servers can still offer benefits over full-fledged API servers due to their simplicity and speed.

Conclusion

Mock servers offer significant advantages in terms of speed, reliability, and flexibility, making them a compelling choice for Appium tests. By reducing dependencies on external services, they help stabilize your test suite and focus on verifying the app’s functionality. Implementing this strategy involves careful setup and maintenance, but the benefits far outweigh the drawbacks, especially for large-scale builds.

𝓗𝒶𝓅𝓅𝓎 𝓉𝓮𝓈𝓉𝒾𝓃𝓰 𝒶𝓃𝒹 𝒹𝓮𝒷𝓊𝓰𝓰𝒾𝓃𝓰!

I welcome any comments and contributions to the subject. Connect with me on LinkedIn, X , GitHub, or Insta.

If you find this post useful, please consider buying me a coffee.

#WomenInTech #Appium #MockServers #MobileTesting #SoftwareTesting #QA #DevLife #TechTips #APITesting

--

--

Lana Begunova
Women in Technology

I am a QA Automation Engineer passionate about discovering new technologies and learning from it. The processes that connect people and tech spark my curiosity.