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:
- Run on a regular Java JVM on your computer.
- They are generally used to test generic Non-Android related classes.
- 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.
- 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.
- Run on an Android Device or Emulator.
- Should be reserved for testing logic that depends on Android APIs or for high-level testing like integration and functional tests.
- 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