Best practices for Android Unit Testing?

你。 提交于 2020-12-28 06:50:28

问题


I am developing a mobile android app. What are the most common libraries/frameworks used for Android unit testing? I feel that most of the business logic, database testing, Web services testing can all be done using JUnit.

However, what's the best approach for testing UI, the UI workflow, etc? For example, how can we test if the Android app launches a web browser successfully? Or how can we confirm if buttons, certain things are pressed successfully? Or if images are loaded successfully?


回答1:


I use JUnit for unit testing and Robolectric for instrumentation tests.

This article shows you a Hello World example with Robolectric

In recent times, I have been researching about integration testing in Android using Arquillian Droidium.

If you want to test some code that consumes a REST API, you can mock it with WireMock. With this library, you can mock REST APIs by code or even deploy a mock HTTP server in your own machine and set up your own mocked mappings.

For REST API mocks, I also recommend you to use Mockable.io.




回答2:


2017 answer

The Android documentation has a topical series called Best Practices for Testing. I would start there.

Local Unit Tests and Instrumented Tests are set up by default when you start a new project. The general advice given is to use the local unit tests whenever possible. Sometimes this requires mocking an object that uses the Android API. The documentation recommends using Mockito in these cases. When UI tests (instrumented tests) need to be done, Android provides the Espresso framework. There are also other tools available, like the Exerciser Monkey (for stress testing) and UI Automator (for testing multiple app interaction).

Example

See this answer for how to get started with tests in Android Studio.




回答3:


There is a guide for automated user interface testing on the Android Developers Website. I haven't tried it but it looks like what you've been looking for.



来源:https://stackoverflow.com/questions/33785761/best-practices-for-android-unit-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!