Detailed explanation of Android unit testing and simulation testing
Testing and Basic Specifications
Why testing is needed?
For stability, we can clearly understand whether the development is completed correctly.
It is easier to maintain and can ensure that the function is not destroyed after modifying the code.
Integrate some tools to standardize development specifications and make the code more stable (such as submitting the unit tests that need to be executed when issuing a diff through phabricator differential, which can ensure the stability of the remote code in the development process).
2. What to test?
General unit testing:
List the exceptions you want to test and verify them.
Performance testing.
Simulation test: According to the needs, test the feedback and display of the interface during the actual use of the user, as well as the application testing of some components that depend on the system architecture.
3. It should be noted that
consider readability, use expressive method names for method names, and consider using a specification for test paradigms, such as RSpec-style. The method name can be in a format, such as: [Method of test]_[Condition of test]_[Expected result].
Don’t use logical flow keywords (If/else, for, do/while, switch/case). In a test method, if you need these, split them into each separate test method.
Test the content that really needs to be tested and the situations that need to be covered. Generally, only the verification output (such as what is displayed and what the value is after a certain operation) is considered.
Consider the time consumption, Android Studio will output the time consumption by default.
There is no need to consider testing private methods. Treat private methods as black box internal components and test the public methods referenced by them; do not consider testing trivial codes such as getters or setters.
Each unit test method should be in no order; decouple as much as possible. For different test methods, there should not be a situation where Test A and Test B have timing.
4. Create a test
Select the corresponding class
Place the cursor on the class name
Press ALT + ENTER
Select Create Test in the pop-up window
Unit testing in Android Studio and Simulation test
control + shift + R (Android Studio’s default execution unit test shortcut key).
1. Local unit testing
Run the test directly on the development machine.
Consider using this type of unit test only when there are no dependencies or only simple Android library dependencies are needed.
./gradlew check
(1) Code storage
If it corresponds to different flavors or build types, add the corresponding suffix directly after test (for example, the unit test code corresponding to myFlavor should be placed in src /testMyFlavor/java below).
src/test/java
(2)Google official recommended reference
1 2 3 4 5 6 7 |
|
(3)JUnit
Annotation
2. Simulation test
A test that needs to be run on an Android device or virtual machine.
Mainly used for testing: unit (unit testing related to Android SDK layer reference relationship), UI, application component integration testing (Service, Content Provider, etc.).
./gradlew connectedAndroidTest
(1) Code storage:
src/androidTest/java
(2) Google official recommendation
1 2 3 4 5 6 7 8 9 10 11 12 |
|
(3) Common UI tests
need to simulate the Android system environment.
Three main points:
Whether the information displayed after the UI is loaded is correct.
Whether the UI information is displayed correctly after a certain user operation.
Display the correct page for users to operate.
(4)Espresso
Google officially provides it for UI interaction testing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Start an Intent that opens the Activity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
(5) Asynchronous interaction
It is recommended to turn off the animation in "Settings->Developer Options" in the device , because these animations may confuse Espresso when detecting asynchronous tasks: Window animation scale, Transition animation scale, Animator duration scale.
For AsyncTask, during testing, if an AsyncTask task is thrown after a click event is triggered, directly onView(withId(R.id.update)).perform(click()) during testing, and then directly detect it. The detection at this time is after AsyncTask#onPostExecute.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
(6) Custom matcher
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
拓展工具
1. AssertJ Android
square/assertj-android
极大的提高可读性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
2. Hamcrest
JavaHamcrest
通过已有的通配方法,快速的对代码条件进行测试
org.hamcrest:hamcrest-junit:(version)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
(1)几个主要的匹配器:
(2)自定义匹配器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
3. Mockito
Mockito
Mock对象,控制其返回值,监控其方法的调用。
org.mockito:mockito-all:(version)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
对访问方法时,传入参数进行快照
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
(1)对于静态的方法的Mock:
可以使用 PowerMock:
org.powermock:powermock-api-mockito:(version) & org.powermock:powermock-module-junit4:(version)(For PowerMockRunner.class)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
或者是封装为非静态,然后用Mockito:
1 2 3 4 |
|
4. Robolectric
Robolectric
让模拟测试直接在开发机上完成,而不需要在Android系统上。所有需要使用到系统架构库的,如(Handler、HandlerThread)都需要使用Robolectric,或者进行模拟测试。
主要是解决模拟测试中耗时的缺陷,模拟测试需要安装以及跑在Android系统上,也就是需要在Android虚拟机或者设备上面,所以十分的耗时。基本上每次来来回回都需要几分钟时间。针对这类问题,业界其实已经有了一个现成的解决方案: Pivotal实验室推出的Robolectric。通过使用Robolectrict模拟Android系统核心库的Shadow Classes的方式,我们可以像写本地测试一样写这类测试,并且直接运行在工作环境的JVM上,十分方便。
5. Robotium
RobotiumTech/robotium
(Integration Tests)模拟用户操作,事件流测试。
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class MyActivityTest{
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
通过模拟用户的操作的行为事件流进行测试,这类测试无法避免需要在虚拟机或者设备上面运行的。是一些用户操作流程与视觉显示强相关的很好的选择。
6. Test Butler
linkedin/test-butler
避免设备/模拟器系统或者环境的错误,导致测试的失败。
通常我们在进行UI测试的时候,会遇到由于模拟器或者设备的错误,如系统的crash、ANR、或是未预期的Wifi、CPU罢工,或者是锁屏,这些外再环境因素导致测试不过。Test-Butler引入就是避免这些环境因素导致UI测试不过。
该库被谷歌官方推荐过,并且收到谷歌工程师的Review。
拓展思路
1. Android Robots
Instrumentation Testing Robots – Jake Wharton
假如我们需要测试: 发送 $42 到 “foo@bar.com”,然后验证是否成功。
(1)通常的做法
(2)Robot思想
在写真正的UI测试的时候,只需要关注要测试什么,而不需要关注需要怎么测试,换句话说就是让测试逻辑与View或Presenter解耦,而与数据产生关系。
首先通过封装一个Robot去处理How的部分:
然后在写测试的时候,只关注需要测试什么:
终的思想原理

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics









