Testing Pyramid in Android

Photo by freestocks on Unsplash

Testing Pyramid in Android

Testing is the process of running or analyzing a system to find any flaws, faults, or missing requirements that are inconsistent with the actual requirements.

Testing is how programmers ensure that their software behaves as expected and solves the correct problems.
The primary goal of testing is to detect software failures so that flaws can be identified and fixed. Testing can only show that a product does not function properly in certain situations; it cannot demonstrate that a product functions properly in all situations.

Testing Pyramid

Mike Cohn, one of the Agile movement's founders, first introduced the testing pyramid in the early 2000s.

The testing pyramid is a guideline that assists in achieving a balance between testing speed and code confidence. When you release software, you want to minimize the possibility of introducing new bugs that break it and affect your users. The lower this risk, the greater your confidence. Following the testing pyramid will allow you to provide cleaner, faster, and more thoroughly tested code.

Testing also offers the following advantages:

  • Rapid feedback on failures.

  • Early failure detection in the development cycle.

  • Safer code refactoring, allows you to optimize code without worrying about regressions.

  • Stable development velocity aids in the reduction of technical debt.

There are three most common types of automated tests: unit, integration, and end-to-end.

Unit tests

Unit tests, which test small units of code, are the most important section at the base of the pyramid. These are extremely fast, so you can run a large number of them without having to sit around and wait for results.

Local tests allow you to evaluate the logic of your app more quickly.

A unit test verifies the behaviour of a small section of code, the unit under test. It accomplishes this by running the code and examining the output.

Software unit tests aid the developer in ensuring that the logic of a piece of software is correct. If you break something, you know about it quickly and clearly. They can’t tell you if everything in your app is working properly because they only test individual classes. This is where integration and system testing come into play.

The Objective of Unit Testing:

The objective of unit testing is to:

  1. separate a piece of code.

  2. confirm the code's accuracy.

  3. evaluate each process and function.

  4. save money and fix bugs early in the development cycle.

  5. make it easier for the developers to comprehend the code base and to quickly implement modifications. facilitate code reuse.

Here are some commonly used unit testing tools:

  1. Junit

  2. Roboelectric

  3. Hamcrest

  4. Mockito - To include a mock object in a local unit test.

  5. We recommend using an assertions library like junit.Assert, Hamcrest, or Truth.

Integration tests

Integration tests are slightly slower than unit tests. Integration tests ensure that interactions between units (classes) work as expected—assuming your unit tests are correct, of course! Integration tests examine interactions between units, so you make fewer assumptions about how well the final application will function. You give up some speed for realism.

Instrumented tests are carried out on Android devices, whether physical or virtual.

Units or individual components of the software are tested in groups during this testing. The integration testing level's goal is to expose flaws during the interaction of integrated components or units.

Commonly used unit testing tools:

  1. AndroidJUnitRunner

  2. Roboelectric

  3. Espresso

  4. UI Automator

  5. Compose test.

UI tests are usually instrumented tests that verify the correct behaviour of the UI.

Guidelines for Integration Testing

  • We only do integration testing after we have completed functional testing on each module of the application.

  • This must always be done module by module so that a correct sequence is followed and no integration scenario is missed.

  • First, determine the test case strategy by which executable test cases can be prepared based on test data.

  • Examine the structure and architecture of the application and identify the critical components.

  • Create test cases to thoroughly verify each interface.

  • Select the input data for the test case execution. Input data is very important in testing.

    If we discover any bugs, we will notify developers, fix the defects, and retest.

  • Perform positive and negative integration testing.

E2E(End-To-End Test)

These are the slowest tests, but they more accurately simulate reality. They eliminate any remaining risk by demonstrating that your application works with real-world users. It ensures that when your app is turned on, users will be able to solve their problems using it.

When performing these tests, it is critical to consider the user's point of view. How would a real person interact with the app? How can that interaction be replicated in tests?

End-to-end tests are at the top of the testing pyramid because they are the most time-consuming to run. They can also be fragile because they must test a wide range of user scenarios. These tests, like integration tests, may require the app to communicate with external dependencies, potentially adding to completion obstructions.


The Android test pyramid is a pattern that will assist you in writing tests that will give you confidence in your code as you make changes. Testing is more than just a way to fix code; it's a way of thinking.