Android automated UI tests: the baby steps

automated UI tests
Summary
  • UI testing simulates user actions to verify correct app behavior, and automating it provides reliable, fast testing that supports faster development, fewer bugs, and more confident refactoring.
  • Frameworks like Espresso or UI Automator can be used for automated UI testing, while Mockito helps mock data to avoid unreliable results from real external APIs.
  • Choosing a test-friendly, modular architecture allows switching between mock data during tests and real data in production.
  • Testing scope is endless, so it's important to decide when existing tests are sufficient to prove the app works as intended.

Since I started building apps I was sure of only two things: one is that I love seeing users enjoying my apps; and the second is that I hate seeing users clicking everywhere and crashing them. So how can I be sure that my users will be able to have a joyful experience (even with those features that are hidden 15 clicks away and that I don’t even remember they exist anymore)? By testing them all!

What is UI testing and why to automate it?

UI testing is nothing more than simulating the user’s environment, performing the available actions and verifying if everything is behaving correctly.

Of course anyone can test features by simply having a human doing this job, but how time-consuming, boring and error-prone can it be? By having automated UI tests we assure that all the features will be tested reliably and quickly. We also become more confident to create or refactor features by being sure that if something gets broken we will know what and where the problem is, leading to faster development, less bugs, and better design decisions.

If it’s not clear yet, by using UI automated tests a specific user’s action or input can be simulated and we can check if it returns the correct UI behavior. Besides that, interactions between our app and a third-one can also be tested to be sure that features like content-sharing are working perfectly. All of it can be achieved by using testing frameworks like Espresso or UI Automator.

Do not get fooled by your data!

It’s also important to use a framework to mock the data while testing the app, like Mockito, for example, since by relying on real APIs during testing we cannot control the scenario, exposing tests to failures in the external APIs. It’s also good to mention that by relying on external APIs we are essentially writing integration tests, which is not our objective here.

Another important point to think about is to choose a test-friendly architecture (aka modular ones) that will allow us to change from mock data during tests to real data during production.

When to stop testing?

A particular point of testing is that it’s endless and it’s very important to decide when and where to stop it, mostly because software has a huge testing scope and, even though it’s possible to cover all the problematic points it can have, it will take like forever. So, it’s needed to get into a point where we can say that the existent tests are enough to prove that the app does work as intended, serving to its proposal.

Conclusion

To wrap it up, keep in mind that automating UI testing is a great way to ensure that actions available in the app will have the expected behavior, which can lead to better user experience, also a more free and enjoyable programming time.

TL;DR

By automating UI testing we simulate the user actions and assure that everything is how we expect. Use testing frameworks to make it easier, like Espresso or UI Automator as well as Mockito to mock your data and not get fooled by unpredictable external APIs content. Embrace the reliability that it can offer and be more confident to do changes in your code. Enjoy!

FAQ

What is UI testing and why should it be automated?

UI testing is simulating the user's environment, performing the available actions, and verifying if everything is behaving correctly. Automating it ensures all features are tested reliably and quickly, avoiding the time-consuming, boring, and error-prone nature of manual testing. It also builds confidence to create or refactor features, leading to faster development, fewer bugs, and better design decisions.

Which frameworks can be used for automated UI testing?

Testing frameworks like Espresso or UI Automator can be used to simulate user actions and check UI behavior, including interactions between your app and third-party apps for features like content-sharing.

Why should data be mocked during UI testing?

Mocking data with a framework like Mockito is important because relying on real APIs during testing means you cannot control the scenario, exposing tests to failures in external APIs. Relying on external APIs also turns the tests into integration tests, which is not the objective of UI testing.

What kind of architecture supports UI testing?

A test-friendly architecture (modular ones) is recommended, as it allows switching from mock data during tests to real data during production.

When should you stop writing UI tests?

Testing is endless, so it's important to decide when to stop. The goal is to reach a point where the existing tests are enough to prove that the app works as intended and serves its purpose, since covering every problematic point would take forever.

About the author.

Francieli Lima
Francieli Lima

Has an incurable crush for experiencing and building fascinating mobile apps, enjoys learning how everything works and how to keep cute flowers alive.