Android Testing Support Library

来源:互联网 发布:安卓好的看书软件 编辑:程序博客网 时间:2024/05/17 16:02

前沿

前两篇只是为自动化做铺垫,开篇有几个问题

1.程序员是不是都不写自动化测试和单元测试?或者写其中一种
2.什么是自动化测试?
3.写自动化测试除了增加工作量之外还能带来那些方便?
4.自动化可以测试那些东西。为什么自动化测试需要程序员写而不是测试人员写?

以上问题都是讨论中经常听到的集中说法。

1.Android Testing Support Library

Espresso

这里写图片描述

摘自官网的翻译:
Android Testing Support Library为Android app的测试提供了一个广泛的框架。 这个库提供了一系列的API可以让你快速build和run你app的代码,它包括了JUnit 4单元测试和功能性的用户界面(UI)测试。你可以通过Android Studio IDE或者命令行的方式运行你使用这些API创建的测试。
2.Testing Support Library的特性
Android Testing Support Library提供包括了以下自动化测试工具:

一,AndroidJUnitRunner:兼容Android的JUnit 4。

二,Espresso:UI测试框架,适用于app内的功能性UI测试。

三,UI Automator:UI测试框架,适用于系统和安装app间跨app的功能性UI测试。

2.1 AndroidJUnitRunner

AndroidJUnitRunner 类是一个 JUnit 测试runner,它可以让你在Android设备上运行JUnit 3或者JUnit 4的测试类,包括使用了 Espresso 和 UI Automator 测试框架的。test runner处理的事情有 加载你的测试package和需要在设备上测试的app,运行你的测试,还有报告测试结果。这个类替换掉只支持JUnit 3测试的InstrumentationTestRunner类。
这也是默认新建的app 在build.config中会默认引入AndroidJUnitRunner 而不是 InstrumentationTestRunner

defaultConfig {     ...     ...     ...     ...    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}

官方关于AndroidJUnitRunner的介绍

地址请看页脚的引用地址
AndroidJUnitRunner is a new unbundled test runner for Android, which is part of the Android Testing Support Library and can be downloaded via the Android Support Repository.
是android 的一个新的非捆绑测试运行器,它是android 测试支持库的一部分
Here are some of the most common features:
下面是一些常用的功能

1.JUnit4 support
2.Instrumentation Registry
3.Test Filters 测试过滤
4.Test timeouts 超时控制
5.Sharding of tests 分片测试
6.RunListener support to hook into the test run life-cycle hook生命周期
7.Activity and Application life-cycle monitoring 生命周期监听
8.Intent Monitoring and Stubbing 意图监控和存根

这里不放demo,原则上 InstrumentationTestRunner 上的testCase 均可以在 AndroidJUnitRunner 的环境下运行。 毕竟 AndroidJUnitRunner 是 AndroidJUnitRunner 的扩展。

2.2 UI Automator 测试框架

UI Automator属于另一种测试模式,可以跨进程。不同的app之间可以使用UiAutoMator 进行测试。 UI Automator放在最后进行说明。

2.3 Espresso

Espresso面向开发人员,他们认为自动化测试是开发生命周期的一个组成部分。虽然它可以用于黑盒测试,但Espresso的全功能由熟悉被测代码库的人解锁。也就是白盒测试

基础包
espresso-core - Contains core and basic View matchers, actions, and assertions. See Basics and Advanced Samples.
核心包,包括一些基本组件

espresso-web - Contains resources for WebView support.
webView 测试包

espresso-idling-resource - Espresso’s mechanism for synchronization with background jobs.
资源异步测试支持包

espresso-contrib - External contributions that contain DatePicker, RecyclerView and Drawer actions, Accessibility checks, and CountingIdlingResource.
一些针对DataPicker ,RecyclerView ,Drawer的action

espresso-intents - Extention to validate and stub Intents, for hermetic testing.
针对espresso-intents 的过滤。

使用Espresso编写简洁,美观,可靠的Android UI测试,如下所示:

@Testpublic void greeterSaysHello() {  onView(withId(R.id.name_field)).perform(typeText("Steve"));  onView(withId(R.id.greet_button)).perform(click());  onView(withText("Hello Steve!")) .check(matches(isDisplayed()));   }

可以说Espresso是最激动人心框架,可以模拟用户行为,进行用例测试。

上面代码解释

1.R.id.name_field 组件输入Steve,
2.点击 R.id.greet_button按钮,
3.判断Hello Steve 组件是否显示在桌面。
这也提现了自动化测试的三个要素,找到元素(onView(),ondata()),执行Action,验证结果是否符合预期(Assert)

接下来正式进入Espresso 的世界

引用

android 的 Testing Support Library 测试支持包(库)
http://blog.csdn.net/u012496940/article/details/49081151
官方网站
https://google.github.io/android-testing-support-library/docs/androidjunitrunner-guide/index.ht

0 0
原创粉丝点击