HomeAndroidGetting Started with Android Testing

Getting Started with Android Testing

While developing Android apps we can use two main types of tests.

  • Unit Tests
  • Connected Tests

The primary distinction between these two are described below:

Unit Tests

  1. Run on a regular Java JVM on your computer.
  2. They are generally used to test generic Non-Android related classes.
  3. Any code that calls the Android API will fail, since Unit tests are run against the mock Android SDK implementation. In this case, one should either a mocking framework like MOCKITO to mock the appropriate Android dependencies or use a Connected test instead.
  4. Additionally you can configure the testing option in the Gradle build script to instruct the mock Android implementation to return the default values rather than throw an error.

Connected Tests

  1. Run on an Android Device or Emulator.
  2. Should be reserved for testing logic that depends on Android APIs or for high-level testing like integration and functional tests.
  3. Connected tests are packaged as an APK that will be installed on an Android device or Emulator. So there is typically more overhead involved in the execution.

In any case, it’s up to the developers to decide how to best write the tests and how to structure the code in such a way to make testing easier.

Both Unit and Connected tests have their own source sets

UNIT = /src/test

CONNECTED = /src/androidTest

 

RELATED ARTICLES

Most Popular