android自动化框架简要剖析(二):Instrumentation+junit.framework.TestCase

android平台测试支撑主架构如下:

1、Instrumentation

java.lang.Object
↳ android.app.Instrumentation  (Android测试环境的核心,通过将主程序和测试程序运行在同一个进程来实现;允许测试用例访问程序的状态及运行时对象)

java.lang.Object
↳ android.app.Instrumentation
↳ android.test.InstrumentationTestRunner    (测试执行类,清单文件中必须配置;我们使用adb shell am instrument.....命令时,必须指定该类,或者自己编写的子类)

2、junit.framework.TestCase   (测试用例和用例集的组织结构,作为框架提供测试API)

 java.lang.Object

↳ junit.framework.Assert
↳ junit.framework.TestCase
↳ android.test.InstrumentationTestCase
↳ android.test.ActivityTestCase
↳ android.test.ActivityInstrumentationTestCase2<T extends android.app.Activity>    (
单元测试时,传入要被测试类;只有apk包时,通过Class.forName()函数获取第一个activity的类名,作为参数)

java.lang.Object
↳ junit.framework.Assert
↳ junit.framework.TestCase
↳ android.test.AndroidTestCase    (测试基类;不能使用Instrumentation框架。但这些类包含访问系统对象(如Context)的方法。使用Context,你可以浏览资源,文件,数据库等等。)

Interfaces:
android.test.PerformanceTestCase


Known Direct Subclasses:
AndroidTestCase,

InstrumentationTestCase,可以使用Instrumentation框架,用于测试Activity。使用Instrumentation,Android可 以向程序发送事件来自动进行UI测试,并可以精确控制Activity的启动,监测Activity生命周期的状态。

TestSuiteBuilder.FailedToCreateTests

Known Indirect Subclasses
ActivityInstrumentationTestCase<T extends Activity>,
ActivityInstrumentationTestCase2<T extends Activity>,
ActivityTestCase,
ActivityUnitTestCase<T extends Activity>,
ApplicationTestCase<T extends Application>,   测试整个应用程序的类。它允许你注入一个模拟的Context到应用程序中,在应用程序启动之前初始化测试参数,并在应用程序结束之后销毁之前检查应用程序。


LoaderTestCase,
ProviderTestCase<T extends ContentProvider>,
ProviderTestCase2<T extends ContentProvider>, 测试单个ContentProvider的类。因为它要求使用MockContentResolver,并注入一个IsolatedContext,因此Provider的测试是与OS孤立的。


ServiceTestCase<T extends Service>, 测试单个Service的类。你可以注入一个模拟的Context或模拟的Application(或者两者),或者让Android为你提供Context和MockApplication。


SingleLaunchActivityTestCase<T extends Activity>,
SyncBaseInstrumentation

原文地址:https://www.cnblogs.com/zhitang2009/p/3423063.html