- Learning Android Application Testing
- Paul Blundell Diego Torres Milano
- 943字
- 2021-07-23 19:58:52
Why, what, how, and when to test?
You should understand that early bug detection saves a huge amount of project resources and reduces software maintenance costs. This is the best known reason to write tests for your software development project. Increased productivity will soon be evident.
Additionally, writing tests will give you a deeper understanding of the requirements and the problem to be solved. You will not be able to write tests for a piece of software you don't understand.
This is also the reason behind the approach of writing tests to clearly understand legacy or third-party code and having the testing infrastructure to confidently change or update the codebase.
The more the code is covered by your tests, the higher the likelihood of discovering hidden bugs.
If, during this coverage analysis, you find that some areas of your code are not exercised, additional tests should be added to cover this code as well.
To help in this request, enter Jacoco (http://www.eclemma.org/jacoco/), an open source toolkit that measures and reports Java code coverage. It supports various coverage types, as follows:
- Class
- Method
- Block
- Line
Coverage reports can also be obtained in different output formats. Jacoco is supported to some degree by the Android framework, and it is possible to build a Jacoco instrumented version of an Android app.
We will be analyzing the use of Jacoco on Android to guide us to full test coverage of our code in Chapter 9, Alternative Testing Tactics.
This screenshot shows how a Jacoco code coverage report is displayed as an HTML file that shows green lines when the code has been tested:

By default, the Jacoco gradle plugin isn't supported in Android Studio; therefore, you cannot see code coverage in your IDE, and so code coverage has to be viewed as separate HTML reports. There are other options available with other plugins such as Atlassian's Clover or Eclipse with EclEmma.
Tests should be automated, and you should run some or all tests every time you introduce a change or addition to your code in order to ensure that all the conditions that were met before are still met, and that the new code satisfies the tests as expected.
This leads us to the introduction of Continuous Integration, which will be discussed in detail in Chapter 5, Discovering Continuous Integration, enabling the automation of tests and the building process.
If you don't use automated testing, it is practically impossible to adopt Continuous Integration as part of the development process, and it is very difficult to ensure that changes would not break existing code.
Having tests stops you from introducing new bugs into already completed features when you touch the code base. These regressions are easily done, and tests are a barrier to this happening. Further, you can now catch and find problems at compile time, that is, when you are developing, rather than receiving them as feedback when your users start complaining.
What to test
Strictly speaking, you should test every statement in your code, but this also depends on different criteria and can be reduced to testing the main path of execution or just some key methods. Usually, there's no need to test something that can't be broken; for example, it usually makes no sense to test getters and setters as you probably won't be testing the Java compiler on your own code, and the compiler would have already performed its tests.
In addition to your domain-specific functional areas that you should test, there are some other areas of an Android application that you should consider. We will be looking at these in the following sections.
You should test whether your activities handle lifecycle events correctly.
If your activity should save its state during the onPause()
or onDestroy()
events and later be able to restore it in onCreate(Bundle
savedInstanceState)
, then you should be able to reproduce and test all these conditions and verify that the state was correctly saved and restored.
Configuration change events should also be tested as some of these events cause the current Activity to be recreated. You should test whether the handling of the event is correct and that the newly created Activity preserves the previous state. Configuration changes are triggered even by a device rotation, so you should test your application's ability to handle these situations.
Database and filesystem operations should be tested to ensure that the operations and any errors are handled correctly. These operations should be tested in isolation at the lower system level, at a higher level through ContentProviders
, or from the application itself.
To test these components in isolation, Android provides some mock objects in the android.test.mock
package. A simple way to think of a mock is as a drop-in replacement for the real object, where you have more control of the object's behavior.
Before shipping your application, you should be sure that all of the different devices it can be run on are supported, or at least you should detect the unsupported situation and take pertinent measures.
The characteristics of the devices that you should test are:
- Network capabilities
- Screen densities
- Screen resolutions
- Screen sizes
- Availability of sensors
- Keyboard and other input devices
- GPS
- External storage
In this respect, an Android emulator can play an important role because it is practically impossible to have access to all of the devices with all of the possible combinations of features, but you can configure emulators for almost every situation. However, as mentioned before, leave your final tests for actual devices where the real users will run the application so you get feedback from a real environment.
- Puppet 4 Essentials(Second Edition)
- 多媒體CAI課件設計與制作導論(第二版)
- 零基礎玩轉區塊鏈
- Android Studio Essentials
- Building Mapping Applications with QGIS
- C語言實驗指導及習題解析
- H5頁面設計:Mugeda版(微課版)
- Learning ArcGIS for Desktop
- 青少年信息學競賽
- Terraform:多云、混合云環境下實現基礎設施即代碼(第2版)
- Building Machine Learning Systems with Python(Second Edition)
- 微信小程序開發與實戰(微課版)
- Python面試通關寶典
- MATLAB 2020 GUI程序設計從入門到精通
- 從零開始學算法:基于Python