Unit Testing Vs. Integration Testing In Android | Hacker Noon

Anu George Hacker Noon profile picture

@anugrgAnu George

Test Automation Geek

Android development has witnessed massive growth in all these years, and any developer who’s worth his salt will thoroughly test his products before launching them into the market. While having a conversation about testing in Android, we often hear two forms of tests doing the rounds — Unit Test and Integration Test. 

In this post, we’ll discuss which test is more ideal or if we should perform both tests before launching our product into the market. Before we proceed, let’s take a look at this testing pyramid.

Photo Source: https://developer.android.com/

This testing pyramid gives us a brief idea of the different types of tests. Unit tests are written by developers, and they’re usually performed in the initial stages (although they can be performed at any point in time throughout the development), while integration tests are performed by testers, and at a later stage of development. Let’s understand them better. 

What Is Unit Testing?

Unit testing is a testing method where developers test the units of components of the code they’ve written. In this method, we can test each and every component of the code to find out if there are any potential errors. The code is generally modular and can be reused, and we can write focused test cases.

Several Android platforms also come with a pre-installed framework that lets developers write unit test codes. Since it’s done in the initial stages of development, it’s practically easy to catch and fix any bugs. Plus, it’s a cost-effective form of testing too. 

For Android unit tests, we can create two forms of tests:

  1. Local tests: As the name suggests, these are compiled to run locally on a JVM, and can reduce the execution time. Most developers use Robolectric if our test cases depend on objects within the Android framework.
  2. Instrumented tests: These are unit tests that are performed on real devices or emulators. These tests are mostly performed when unit tests have complicated Android dependencies. 

Based on our requirements, we can pick either of the tests. One works well for testing on local machines, and the other for real devices or emulators.

Photo Source: https://philmikejones.me/blog/unit-testing-in-r/

Features Of Unit Testing

i. Unit tests can be run in isolation, without dependencies on other modules, and because of that, the testing method pays a meticulous amount of attention to the behaviour of independent components.

ii. Since the tests are written and conducted before the code goes into production, it can follow Test Driven Development.

iii. While external dependencies can be mocked in unit testing, we cannot be sure if the code works the way it should with external dependencies.

iv. Unit testing is cost-effective and can be performed at any point in time during development.

v. Several unit tests can run parallelly because every test is run in isolation.

vi. You can unit test different components or a part of your code.

What Is Integration Testing?

Integration testing is a form of the testing method where several individual components are put together (or integrated) and tested together. Performed by testers, this testing method can detect any app-wide bugs or even bugs that unit testing may have missed. This is because, when all components come together, they may not perform well together.

For performing integration testing on Android devices, we have to check for components like Service, Activities, and Content Provider. These are basically to check how the actions, services, or content work on different Android devices. 

Features Of Integration Testing

i. Great for checking how the components interact with frameworks and other components.

ii. Integration testing runs end-to-end tests for any given product and app, and hence it’s capable of catching app-wide bugs.

iii. With Integration testing, we’ll have a clear picture of how our code functions when it comes in contact with external dependencies. 

iv. The tests may not run parallelly because tests may depend on each other or can share the same pre-state setup.

v. Integration testing doesn’t follow Test-Driven Development approach because the testing happens after the development.

vi. Integration testing is relatively expensive because it’s difficult to trace the bugs. 

Integration tests can also be performed on real devices, which means we can find out how our app or website performs on our end user’s device. But this can tamper with the speed, and won’t be relatively as fast as unit testing. 

Testing In Android: Unit Or Integration Or Both?

Integration tests don’t run as quickly as unit tests, and this could pose a predicament when it comes to delivery timelines. For instance, if we’re working on a project with a deadline, Integration testing isn’t agile and if we have to run 50-60 integration tests, it may take anywhere between 20-30 minutes for the build. Most developers prefer unit testing because of this very drawback and because bugs can be detected in the early stages. 

Now the question remains — should we go for unit testing or integration testing? What if we say both? 

For instance, let’s take a look at the following image.

Picture Source: https://www.reddit.com/r/ProgrammerHumor/comments/9dw6kw/had_to_explain_to_a_colleague_why_integration/

From the image, it can be inferred that unit testing individual components lead to a good unit test; performing an integration testing of all the components together leads to good integration testing. But from the third section, it can be noted that for a good product, we have to thoroughly unit test both the components and their linkage. Otherwise, it can lead to a broken app or product. 

To summarize, When either the first or the third section aren’t done properly, we might encounter bugs. The second image is the apt representation of the fact that we’ll need to perform both unit testing and integration testing for a solid product.

For apps or websites to perform the way they’re designed to perform, it’s important for developers to unit test their code before sending it out to the testers for integration testing. Integration testing also adds a layer of security, and it helps us catch any bugs that we might have missed during unit testing. 

Integration Testing In Multiple Environments?

There’s nothing much we could do to improve the speed of a suite of integration tests. However, multiple suites can be executed parallelly, provided we have a competent tool.

There are some popular mobile testing tools. One solution is Testsigma, an automated mobile app testing tool that understands the need for speed and performs tests on many Android, iOS devices seamlessly.

Also, runs the tests in parallel, thereby cutting down the execution time. Besides, all tests run on a cloud, manages tests suites and executions via a test lab on the cloud. You can also create test cases, categorize, and schedule them as per your requirements.


We did see that only unit testing may not help you find the bugs that you might find during integration testing and vice versa. Thus, for releasing a product that boasts of good overall quality, it is essential to maintain a healthy balance between unit testing and integration testing.


Join Hacker Noon

Create your free account to unlock your custom reading experience.

read original article here