4 Fundamentals of Android Testing

Android users use apps in different ways. A shopping app, for example, allows shoppers to filter products according to categories. This app also lets users save products in a wish list or subscribe to an online catalog. Not to mention the volume of traffic that one app needs to cater to. 

Android developers consider all these and more especially in the testing stage of the apps. 

Android Testing Today

During this stage, a set of testers or QAs take over and put the app into a series of quality testing. Quality testing can be done through two methods– manual testing and automated testing.

Manual testing, from the name itself, requires manual labor thus making it more tedious to execute. On the other hand, automation testing involves faster processes that can cover wider app features. 

Automation testing provides a wide range of advantages that is why this industry is expected to grow more in the following years. By 2023, this industry will be worth around $20billion USD

So, more testers or QAs are now automating their testing processes. For individuals who are just starting to learn this method, there are various resources on automation testing for beginners

At the end of the day, both testing methods are vital to the app’s success, and testers live by these four fundamental aspects of Android app testing.

  • Code Organization

When you develop app features repetitively, you create a new test as well. Or you introduce new cases or assertions to your unit tests. You may encounter failure at the beginning as a feature is not yet implemented. This is where code organization comes in handy.

Consider any emerging units of responsibility when adding a new feature. Based on the single-responsibility principle or SRP, every module, function, or class in a program is responsible for a part of the program’s functionality. It must encapsulate the unit or part.

For every unit, write a matching unit test. These tests must consider all potential user interactions such as standard user interactions, incorrect or invalid inputs, and interactions when resources are unavailable.

Your App as a Set of Modules

Develop codes in modules. Viewing an app as a chain of modules makes it easier and more organized to test a code. Each one is for a particular task which users perform when interacting with the app. 

Create boundaries for every module and grow new modules to scale your app. Every module must have a particular focus. APIs for interactions between modules must be consistent and seamless.

  • Test Environment Configuration

After organizing your codes, it’s time to configure the testing environment. Consider the following practices.

Arrange Test Directories According to Environment

Android Studio, the official app building tool for Android devices, offers execution environments to help developers organize tests. There are two directories to use: the androidTest directory and the test directory.

androidTest directory runs tests on real-life and virtual devices. End-to-end testing, integration testing, and tests that require further validation other than JVM can use androidTest. Meanwhile, the test directory runs unit tests or those validated using your machine.

It can take more time, but running tests on an actual device is essential for Android app testing. Other than an actual device, you may also test with a virtual device like an Android Studio emulator or a simulated device.

From the three, real devices produce the highest fidelity. Simulated devices offer faster testing but provide lower fidelity. Finally, virtual devices are precise and quick, but it’s always best to capture snapshots of your setups to streamline testing.

  • Writing Tests

Now that you are ready with the ideal testing environment, you may write tests to assess functionality. Tests are classified as small, medium, and large-scale.

Unit Testing – Small Test

Small-scale tests assess the behavior of the app for every class. Small tests are also unit tests that have the lowest fidelity but are the quickest to execute. Small tests are focused unit tests that validate the function and contracts of every class in the app.

Using Local Unit Testing

Consider AndroidX Test APIs so unit tests can run on an emulator or device. Tests that run on JVM development machines, Roboelectric is a better choice as it simulates runtime for API level 16 or higher. It also offers community-maintained fakes or shadows. There’s no need to test codes that depend on a framework minus an emulator. 

Using Instrumented Unit Testing

This type of small test is performed on an emulator or device. This test takes time to execute compared to local unit tests, but it is the choice to evaluate the app’s behavior to actual hardware.

AndroidX Test is the best for performing instrumented testing. This tool uses the main thread (UI thread or activity thread), where the UI interactions and the activity lifecycle events happen. AndroidX Test also uses the instrumentation thread where tests are run. 

Integration Testing – Medium Tests

Medium-scale tests assess the relationship between the different parts of the stack in a module. It also evaluates the interactions between interrelated modules.

Medium tests are module-level tests, so you’ll be using the app’s structure. Here, you’re defining the ideal way to identify groups of units within the app. You can conduct tests on the interactions of a view and a view model, testing from the repository layer, interactions within the app stack layer, and multi-fragmented tests.

UI Testing – Large Tests

Large-scale tests assess the interactions between multiple modules within the app. These are end-to-end tests with the highest fidelity but take a lot of time.

Large tests prevent congestion in your codes. Small and straightforward apps may need a single large test to check functionality. Meanwhile, larger, more complex apps will need larger test suites according to verticals, ownership, and user goals.

More on App Testing Frameworks

Android app developers prefer to test their apps on a cloud-based tool or emulated device than an actual physical device. A service like Firebase Test Lab allows you to test using different hardware and screen sizes quickly and safely. 

Another open-source android testing framework is Espresso. This tool supports medium testing and synchronization in large-scale testing. Espresso will help you track extended background operations, conduct off-device testing, and complete workflows that are beyond the app’s processes.

  • Refining App Testing

AndroidX Test is a new technique of code testing. It is an open-source tool that helps developers create, test, and release code libraries. Think of AndroidX Test as an upgrade to the traditional Android Support Library. Developers can use various elements of the AndroidX Test to enhance app testing processes further.

Truth is a library of assertions and helps create readable assertions and possible failure messages. Use Truth to replace Hamcrest-based or Junit-based assertions as you start the validation part of the test.

Espresso is another tool used for UI testing. You can start accessibility checks on your app by enabling it from the suite’s setup.  You can find out more about interpreting results of accessibility checks from Espresso through its checking guide.

After an accessibility check, Espresso will present some opportunities to enhance the accessibility of your app. However, you cannot address it ASAP, but you can stop the tool from failing because of its results by temporarily ignoring these. As you do this, don’t ignore other similar results.  Use a matcher to allow Espresso to suppress a specific outcome as it completes your app’s accessibility check.

Final Words

Android testing, both manual and automated, ensures that final results can cater to the users’ activities. Just like any product or service, testing preserves the quality and the efficiency of these apps.

Leave a Comment