当前位置:   article > 正文

Android 单元测试(四) 之AndroidJUnitRunner基础

androidjunitrunner

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());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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


    /**
     * Returns the instrumentation currently running. Use this to get an {@link Instrumentation}
     * into your test.
     *
     * @throws IllegalStateException if instrumentation hasn&#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/297634
推荐阅读
相关标签
  

闽ICP备14008679号