按测试规模从小到大,测试可以分为三个层级:Unit Tests, Integration Tests, UI Tests
按测试运行环境不同,测试可分为两个环境:Local unit tests, Instrumented unit tests
测试规模分类
Unit Tests
- Robolectric
- Mockito
- Instrumented unit tests
Integration Tests
多单元组成的组件测试,包含service tests, integration tests, and hermetic UI tests
UI Tests
AndroidJUnitRunner使测试得以运行在Android设备上,其支持以下测试工具:
- JUnit4 Rules: 管理测试关联组件的生命周期,如activity和service。
- Espresso:管理应用内的交互
- UI Automator:当测试需要与系统交互时,才会考虑使用UI Automator(考虑系统版本变更导致的工作量)。
测试环境分类
Local unit tests 本地单元测试
使用最流行的Java单元测试框架JUnit4;
当测试对象对Android framework有依赖时,可使用Mockito或其它工具模拟这些Android framework对象。
Instrumented unit tests 仪器测试
测试运行在Android设备或虚拟机上
仪表测试需要一个JUnit test runner(AndroidJUnitRunner)和测试APIs提供者(Espresso, UI Automator)