Android TDD Part 13 — Summary of testing

Evan Chen
2 min readMay 28, 2022

--

Let’s summarize the test on Android. Android tests include :

  • Local Unit Test
  • Local Unit Test by Robolectric
  • Instrumented Test
  • UI Test

Local Unit Test

The local unit test is the fastest. Always prefer unit tests to others.To write a good unit test, you should separate UI and business logic.

Instrumented Test

While test subjects are related to the Android framework, you need to use Instrumented tests. These tests are run on emulators or devices.

UI Test

UI tests focus on user behavior. It also runs on emulators or devices.

Local unit test by Robolectric

Robolectric provides a way to let you run tests on JVM when you are using Android API.

Instrumented tests V.S. UI Test

These two tests are both located in the “androidTest” folder and run on emulators or devices. What’s different between Instrumented Tests and UI tests is that UI testing is more specific to user behavior. So if you want to test about SharedPreference, it’s an Instrumented test. If you want to test what users do, it’s a UI test.

Espresso V.S. Robolectric

Espresso and Robolectric are both used to test UI. What’s different between them is:

Espresso: the folder is in “Android Test”.
Robolectric: the folder is in “test”.

Espresso: tests run on devices.
Robolectric: tests run on JVM.

The more significant difference is the behavior of testing. For example, if You have a SignUpActivity and ResultActivity, if sign up successfully, should direct to ResultActivity and display “Sign up success”.Then Espresso and Robolectric will test different items:

Espresso : “Sign up success” should shown in ResultActivity. See TDD part 9:Espresso.
Robolectric : Intent should be correct. See TDD part 10: Robolectric.

Another difference is that UI tests do have “type words” on the app, Robolectric only set the value on EditText. So UI tests will open the keyboard to type words. It’s more close to user behavior. It’s more like a user really do.

--

--