AndroidJUnitRunner,Google官方的android单元测试框架之一,适用于 Android 且与 JUnit 4 兼容的测试运行器!测试运行器可以将测试软件包和要测试的应用加载到设备、运行测试并报告测试结果。
此测试运行器的主要功能包括:
要求 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 特定的注解,包括:
测试分片
测试运行器支持将单一测试套件拆分成多个碎片,因此您可以将属于同一碎片的测试作为一个组在同一 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");
}
}