https://github.com/bboyfeiyu/android-tech-frontier/tree/master/android-blog/Google%2B%20%E5%9B%A2%E9%98%9F%E7% 9a%84%20android%20ui%20%e6%b5%8b%e8%af%95
Google + team's Android UI test
- The original link: how the Google + Team Tests Mobile Apps
- Translator: Allenlsy
- Translator's blog address: http://allenlsy.com/android-ui-tests-in-google-plus-team/
- Proofread by:
Android testing is divided into 3 main types:
Unit Test
Distinguishing between UI code and feature code is especially difficult in Android development. Because sometimes activity has both Controller function and view function. Robolectric is an excellent Android testing framework that provides a stub for an Android framework so that the test run is actually run on the JVM, Instead of running tests on Android platforms such as Robotium and instrumentation, this improves speed. Please also refer to Gradle support for Unit tests.
Enclosing UI tests (Hermetic UI test)
This test method makes the test not require external dependencies and network requests. The main purpose of this is to improve the speed of testing and reduce the external impact of testing, after all, network calls are relatively slow. Espresso can be used to impersonate a user's UI action.
Monkey Test
Monkey test is like a monkey in the test app, without any regularity in your app on the HU-click. When the computer runs Monkey test, it can make thousands of UI actions per second (this frequency can be configured), such as clicking and dragging. So this test can be considered a stress test, used to detect ANR.
The Google + team summarizes some of the experience and strategies of UI testing.
Strategy 1: Do not use the End-to-end test as a UI test
Let's look at some definitions:UI testing is to ensure that the app can return the correct UI output for user UI actions. end-to-end Testing (e2e test) is to test the entire system through interaction between the client and the background server. The following diagram shows the test steps:
Usually do the UI test, you need the background server, so may generate network calls. So the UI test is like the e2e test. However, there are many difficulties in the E2E test:
- Slow test speed
- Network request will fail
- Difficult to debug
Here's a look at how to fix these problems.
Strategy 2: Use pseudo-servers for closed UI testing
In this strategy, you can avoid network requests through fake backend servers, as well as other external dependencies. Technically, you'll need to provide the return data locally in the app. There are many ways to do this, such as manually making a network request, saving the response, and repeating the response during the test. So you do a fake server that's locked down locally.
When you have this pseudo-server, you also need to write tests for this pseudo-server. So this is, your e2e test is divided into server testing, client testing and integration testing.
Now for such a solution, you need to maintain your own pseudo server, local database and tests.
Here is an example diagram of the E2E test:
This is a closed UI test that uses a pseudo-server
The difference is that several data sources for Frontend server have changed. From the original real back end, turned into a closed server, or a mock server. This is useful when testing calls to the network API.
Strategy 3: Use Dependency Injection
Dependency Injection (Dependency injection) can help generate test data. I recommend using dagger as a dependency injection framework.
Dependency injection can be used to generate false data in both the UI test and the unit test. In the instrumentation test framework, the test apk file and the app running at test time are under the same process, so the test code can invoke the app code. You can also overwrite the app's classpath to inject fake data in this way. For example, you can use dependency injection to forge an implementation of a network connection that can provide false data when the network connection is invoked.
Strategy 4: Divide the app into small libraries
This is a better way to modularize your app. After your app is divided into smaller class libraries, you can add their own UI dependencies or Gradle library dependencies for these class libraries.
When you have your own library and provide support for dependency injection, you can write test apps for each library. Finally, the integration test can be written to ensure that the class library is working directly with the correct.
For example, we have a login function library, then I can write a test app only for this login function library:
Summarize:
- Do not use E2E tests instead of UI tests. A better approach is to use unit testing + integration Testing + UI testing.
- Using a closed test policy
- Using Dependency Injection
- Divide the app into different small group libraries, write tests separately, and then write integration tests to ensure that the interactions between the components are correct.
- Modular UI testing has proven to be faster and more stable than e2e testing. This kind of test can greatly improve the development efficiency.
Google + team's Android UI test