1 Activity测试:基于Android instrumentation框架的测试。
Android提供的基础类是InstrumentationTestCase。主要的两个子类是ActivityInstrumentationTestCase2和ActivityUnitTestCase.
测试点:
- Input Validation: 包括对一个EditText的输入值检查,button的有效性验证,error messages是否正常提示。
- Lifecycle events: 包括当屏幕旋转时,app的activity状态是否可以保持
- Intents: 包括测试app是否可以正常处理intent filter里面所有功能
- Runtime configuration changes: 包括在app运行时,屏幕旋转或者语言的切换等是否导致app异常
- Screen sizes and resolutions: 包括所有支持的设备尺寸的测试
2 Service测试:Android提供了mock对象,针对Service进行测试。
Android提供的主要测试类是ServiceTestCase
测试点:
- 确保onCreate()和onDestory()在service启动和停止时调用了。
- 确保在调用Context.startService()后,Service能够正常处理多个请求。多个Context.startService()调用只会触发一个Service.onCreate().
- 测试Service的业务逻辑,包括异常值,算法等。
3 Content Provider测试:Content provider提供不同进程间数据交互的标准化接口。
Android提供的主要测试类是ProviderTestCase2
测试点:
- Test with resolver methods
- Test a public provider as a contract:包括测试provider暴露出来的常量,测试provider提供的所有URIs,测试无效的URIs.
- Test the standard provider interactions:大部分provider包括如下方法:query, insert, delete, update, getType, and onCreate()。针对这些方法进行验证。
- Test business logic
4 需要注意的测试内容
- Change in orientation:设备方向的改变
- Change in configuration: 系统语言和键盘的改变
- Battery life:电池的消耗
- Dependence on external resources:外部资源包括network, SMS, Bluetooth, GPS的可用性
5 Reference:
http://www.android-doc.com/tools/testing/what_to_test.html
http://www.android-doc.com/reference/junit/framework/TestCase.html
如下图,测试类的继承关系: