How to test Android Apps

Testing

We started building a new Android app, based on a previous version with reliability issues. Even with a tight deadline, we wanted to make sure we covered it with a pragmatic test suite.

Easier said than done. In the last two months we've been learning a lot about Android architectures and testing. There are many different tools, for different purposes (a good thing, but confusing for someone who's starting). Those tools are evolving fast, and documentation gets left behind, generating confusion. The Android framework itself is not optimally designed for testing. But things are definitely improving.

With this article, we're starting a series of How to test Android Apps articles. The goal is to help make testing more common in Android development. Which increases the chance that the next project we get already has tests in place.

Configuration

Android Studio come with built-in testing support.

Pick the class you want to test, in our example we will be using the class GetAndSetCounter, right click and you should see the following:

Screenshot_2021-06-24_at_11.44.42.png

Selecting Test should get you a "Create Test" prompt like so:

Screenshot_2021-06-24_at_11.45.47.png

Analyzing the prompt we have:

  • Testing Library: picking the Test Framework, we tend to use JUnit.
  • Class name: the name of the testing class
  • Superclass: for the Testing Class that will be created
  • Destination Package: where the test file will be located, default will be a mirror package for testing, it's useful because it allows you to access package-private variables.
  • Generate:
    • setUp/@Before: a method that will run before each test
    • tearDown/@After: a method that will run after each test
  • Generate test methods for: by picking the options here the Test class will come with empty @Test methods.

note: Tests are marked with the @Test annotation. You can use @Before and @After to run code around each test, and @BeforeClass and @AfterClass to run code around all tests inside the test class.

After this configuration you press OK, and get confronted with the following:

Screenshot_2021-06-24_at_11.54.52.png

This is a very important choice. Here you will have to pick between AndroidTest (1st option) and Test (2nd option).

  • AndroidTest is an Instrumentation Test. Instrumented unit tests are tests that run on physical devices or emulators, and they can use the Android framework APIs.
  • Test is a simple Unit Test that runs your computer's JVM, as such it has no access to the Android framework.

Which one to use?

If you only need unit tests, go with the second option. Tests start much faster, since it doesn't upload and install the app on a device every time. But if you need integration tests or UI tests as well, go with Android Instrumentation Tests.

There's also a popular way to run instrumentation tests without a device, Robolectric, which we'll cover later on, but it's not without limitations.

Next up, we will talk about Architecture. It influences how easy testing your application will be.


How to test Android Apps

  1. Introduction
  2. Architecture
  3. Unit Testing
  4. Instrumentation Testing
  5. Other details
DevelopmentSérgiotesting, how to