AndroidJUnitRunner,Google官方的android单元测试框架之一,适用于 Android 且与 JUnit 4 兼容的测试运行器!测试运行器可以将测试软件包和要测试的应用加载到设备、运行测试并报告测试结果。
此测试运行器的主要功能包括:

  • JUnit 支持
  • 访问仪器信息
  • 测试筛选
  • 测试分片

要求 Android 2.2(API 级别 8)或更高版本。

JUnit 支持
测试运行器与 JUnit 3 和 JUnit 4(最高版本为 JUnit 4.10)测试兼容。使用时不要混用JUnit 3 和 JUnit 4 测试代码。如果要创建一个 JUnit 4 仪器测试类以在设备或模拟器上运行,则测试类必须以 @RunWith(AndroidJUnit4.class) 注解作为前缀。
如下是一个验证包名的JUnit 4 仪器:

@RunWith(AndroidJUnit4.class)public class ExampleInstrumentedTest {    @Test    public void useAppContext() throws Exception {        // Context of the app under test.        Context appContext = InstrumentationRegistry.getTargetContext();        assertEquals("com.yangge.myapplication", appContext.getPackageName());    }}

访问仪器信息
可以使用 InstrumentationRegistry 类访问与测试运行相关的信息。
如 Instrumentation 对象:

    /**     * Returns the instrumentation currently running. Use this to get an {@link Instrumentation}     * into your test.     *     * @throws IllegalStateException if instrumentation hasn't been registered     */    public static Instrumentation getInstrumentation() {        Instrumentation instance = sInstrumentationRef.get();        if (null == instance) {            throw new IllegalStateException("No instrumentation registered! "                    + "Must run under a registering instrumentation.");        }        return instance;    }    //使用时直接调用该静态方法即可:InstrumentationRegistry.getInstrumentation()

目标应用 Context 对象:

    /**     * Return a Context for the target application being instrumented. Use this to get a     * {@link Context} representing {@link Instrumentation#getTargetContext()} into your test.     */    public static Context getTargetContext() {        return getInstrumentation().getTargetContext();    }

测试应用 Context 对象InstrumentationRegistry.getContext()、传递到测试中的命令行参数InstrumentationRegistry.getArguments() 等。使用 UI Automator 框架编写测试或编写依赖于 Instrumentation 或 Context 对象的测试时,此数据非常有用。

测试筛选
在 JUnit 4.x 测试中,您可以使用注解对测试运行进行配置。此功能可将向测试中添加样板文件和条件代码的需求降至最低。除了 JUnit 4 支持的标准注解外,测试运行器还支持 Android 特定的注解,包括:

  • @RequiresDevice:指定测试仅在物理设备而不在模拟器上运行。
  • @SdkSupress:禁止在低于给定级别的 Android API 级别上运行测试。例如,要禁止在低于 18 的所有 API 级别上运行测试,请使用注解 @SDKSupress(minSdkVersion=18)。
  • @SmallTest、@MediumTest 和 @LargeTest:指定测试的运行时长以及运行频率。

测试分片
测试运行器支持将单一测试套件拆分成多个碎片,因此您可以将属于同一碎片的测试作为一个组在同一 Instrumentation 实例下运行。每个分片由一个索引号进行标识。运行测试时,使用 -e numShards 选项指定要创建的独立分片数量,并使用 -e shardIndex 选项指定要运行哪个分片。

例如,要将测试套件拆分成 10 个分片,且仅运行第二个碎片中的测试,请使用以下命令:adb shell am instrument -w -e numShards 10 -e shardIndex 2

AndroidJUnitRunner本质上不算是个测试工具,它只是Google基于JUnit 针对Anroid封装的一个测试用例运行器而已。至于它用来运行Espesso还是Uiautomator的用例都是可以的。

JUnit4的使用参见:
Android 单元测试(一) 之JUnit基础
Android 单元测试(二) 之JUnit进阶
下面是一个测试样例:

@RunWith(AndroidJUnit4.class)public class ExampleInstrumentedTest {    @Before    public void testBefore() throws Exception {        LogUtil.e("JUnit","testBefore");    }    @Test    public void useAppContext() throws Exception {        // Context of the app under test.        Context appContext = InstrumentationRegistry.getTargetContext();        LogUtil.e("JUnit","testTest");        assertEquals("com.yangge.myapplication", appContext.getPackageName());    }    @Test    public void testTest() throws Exception {        LogUtil.e("JUnit","testTest");    }    @Test    @SmallTest    public void testTestSmallTest() throws Exception {        LogUtil.e("JUnit","testTestSmallTest");    }    @SmallTest    public void testSmallTest() throws Exception {        LogUtil.e("JUnit","testSmallTest");    }    @MediumTest    public void testMediumTest() throws Exception {        LogUtil.e("JUnit","testMediumTest");    }    @LargeTest    public void testLargeTest() throws Exception {        LogUtil.e("JUnit","testLargeTest");    }    @RequiresDevice    public void testRequiresDevice() throws Exception {        LogUtil.e("JUnit","testRequiresDevice");    }    @SdkSuppress(minSdkVersion = 19)    public void testSdkSuppress() throws Exception {        LogUtil.e("JUnit","testSdkSuppress");    }    @After    public void testAfter() throws Exception {        LogUtil.e("JUnit","testAfter");    }}

更多相关文章

  1. Android牟利之道(一)--界面嵌入有米广告
  2. Android自动化测试框架Robotium
  3. Android下Protobuff框架性能测试结果
  4. Android兼容性测试工具Spoon
  5. android 常用测试框架
  6. 浅入浅出 Android(安卓)安全:第三章 Android(安卓)本地用户空间层
  7. android service
  8. Android:单元测试Junit的配置
  9. spring for android

随机推荐

  1. Android 上的 制表符(tab) —— 一个神奇的
  2. 2015年 代做安卓毕业设计 Android毕业设
  3. [翻译]Android教程-保存数据-支持不同的
  4. Android读取工程内嵌资源文件的两种方法
  5. Android Intent:不同应用程序之间通信的桥
  6. 关于Android WebView内容不同屏幕兼容处
  7. android基于ffmpeg本地视频、在线视频、
  8. sqlite cursor
  9. Android社交系统
  10. 210开发板Android系统串口程序