这两天准备研究Robotium的源码,不过由于Robotium是以Instrumentation为基础的,因此就先补习了一下Instrumentation的基础。
Instrumentation是Android自带一个单元测试框架,不过虽然这么说,其对于大部分应用开发人员来讲,最大的作用反而是用于功能或UI测试。
整个框架继承自JUnit框架,使用方法也类似,也是要继承TestCase类。不过在Instrumentation中,需要继承的是几个Google提供的TestCase的子类,其中最重要也是最常用的是ActivityInstrumentationTestCase2这个类,一般进行应用界面测试时都需继承此类(Robotium一般也是使用此类)。当然,Android也提供了几个其他选择用于测试其他组件:
- ActivityUnitTestCase: 用于单独Activity的单元测试
- ApplicationTestCase:用于测试Application
- ProviderTestCase2:用于测试Provider的测试类