本文半翻译与google官方文档,在学习之后,需要了解的是android提供的测试库工具,以及了解它们的作用,以前都是用junit3写robotium的,现在与时俱进吧。

android提供多重测试用的支持库,比如说junit4的支持以及ui测试。这一篇文档告诉我们android测试支持库提供了什么样的工具以及怎么使用它们。
android测试支持库包含了下面几个自动化工具:
AndroidJunitRunner 为android提供junit4支持
Espresso 测试框架
Ui Automator 测试框架

AndroidJunitRunner:

这个类支持Espresso和Ui Automator测试框架,运行并监控你的测试结果,这是用来替代只能运行junit3的instrumentationTestunner的。(这个可以导出报告,非常方便,junit4确实很方便),有如下优点:

1.Junit 支持
支持junit3和junit4,但是不推荐混合编写,如果你用junit4的格式,在类前面声明
@RunWith(AndroidJunit4.class)。

2.使用instrumentation特性
你可以使用 InstrumentationRegistry这个类获取你的测试相关信息,这个类就是包含Instrumentation对象的和Context对象,这些对象对你写测试用例是很有用的。

3.测试过滤器
使用annotations多重标签测试,这个特性会减少你的代码标记,除了junit4特有的标记,还可以使用android新增的特性。
比如说:
@RequiresDevice
只能运行在真机中
@sdkSupress
@SDKSupress(minSdkVersion=18).最低运行版本
@smallTest @MediumTest @LargeTest

4.测试分享
就是一个用例支持跑多台机器


测试依赖框架配置
测试支持库是可以通过Android SDK Manager下载的:
1. Android Support Repository item.
2. Click the Install packages… button.
下载完后测试依赖库都是在android.support.test包下面。所以要使用这些库,需要在gradle中添加

dependencies {  androidTestCompile 'com.android.support.test:runner:0.4'  // Set this dependency to use JUnit 4 rules  androidTestCompile 'com.android.support.test:rules:0.4'  // Set this dependency to build and run Espresso tests  androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'  // Set this dependency to build and run UI Automator tests  androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'}
android {    defaultConfig {        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }}

Espresso:

谷歌其实说了Espresso是做白盒测试适用的。
同步UI线程
之前的测试框架都是需要等view出现才能够执行和断言,解决办法是延迟xx秒。但是Espresso解决了Instrumentation和UI线程之间的同步问题,可以让你在执行之前不用sleep了。

UI Automator:

谷歌推荐用来做黑盒自动化,具体用法就不说了。

总结:

本篇文档介绍了android.support.test包中,也就是android提供的测试支持库,其中androidjunitrunner是谷歌专门为android开发的用来替代InstrumentationTestRunner的类,允许你在android测试中用junit4,并且增加了独有的annotation标记,android的测试用例都支持androidjunit。后面介绍了两个自动化框架,espresso和uiautomator,其中espresso解决了UI线程同步的问题。官方建议是前者更适合做白盒测试后者更适合做黑盒自动化。

更多相关文章

  1. Android:在子线程中更新UI,解析异步消息处理机制(Handler)
  2. android客户端—AsyncTask的使用
  3. android studio 2.2单元测试
  4. 重磅来袭!2020 年需要关注的 5 大 Android(安卓)开发技术,抓破脑袋
  5. Android文件下载之进度检测
  6. Android消息处理机制——Looper、Handler、Message 源码分析
  7. Android以太网框架情景分析之启动简介
  8. Android应用框架之PackageManagerService
  9. PhoneGap移动开发框架

随机推荐

  1. Android 添加图片水印(图片+文字)
  2. Android 根据Video绝对路径获取其缩略图
  3. 浅析:Pulltorefresh使用中碰到的问题
  4. Android的平台架构
  5. INSTALL_FAILED_CONFLICTING_PROVIDER 安
  6. Android中使用全屏
  7. 《Android开发从零开始》——10. LinearL
  8. android settings命令
  9. [置顶] android调用第三方库——第一篇
  10. eclipse报错:This Android SDK requires A