Android unit test case automation: Robolectric library vs Android Testing framework

后端 未结 2 1386
清酒与你
清酒与你 2021-01-30 09:06

Wondering which one is the better choice to write unit test cases for android apps and libraries: Using Robolectric library or sticking with Android Testing framework. I want to

相关标签:
2条回答
  • 2021-01-30 09:13

    I worked on both, what i found is :-

    1) Robolectric do not support API 19, it's mention in its document - http://robolectric.org/eclipse-quick-start/. It's a great disadvantage of it.

    2) Robolectric run on JVM not on DVM. So we can not detect that on that particular time GPS is enable in device or not etc. We can only pass our pre-decided value for it.

    3) Code writing in Robolectric is complex than junit specially for fragment there are lot of complexity and issues.

    4) Robolectric needs external jar and configuration and for junit test we do not need any external library.

    5) Robolectric is faster because it runs on JVM but this have disadvantage too, we can not see UI on our device, what screen code is executing.

    For Android, i like jUnit test.

    0 讨论(0)
  • 2021-01-30 09:30

    I use a tiered system, where I prefer earlier tiers where possible:

    1. Pure unit tests. I try to make as much code as possible fully independent of Android APIs, and then use "pure" unit tests which can run on any JVM. These tests are the fastest, and it helps keep code that has no need to be Android-specific portable.
    2. Robolectric-supported unit tests. Where my code has only small dependencies on Android APIs, that can be satisfied by Robolectric shadows, I test it with Robolectric. There is a little more setup time for Robolectric compared to pure tests, but it's still faster than starting/running on an emulator.
    3. Android framework tests. Where Robolectric doesn't cut it - either because the shadows don't exist, or because I'm heavily using Android APIs (and therefore want to test against the Real Thing) - I write test that run on the emulator/device with the default framework.

    The point of the tiers is to keep things as simple as possible, which keeps the full suite faster and helps promote cleaner code.

    0 讨论(0)
提交回复
热议问题