引言

Espresso和Robotium都是android UI自动化测试框架,且都是建立在Android Instrument的基础之上。对于测试人员来说,UI测试应该具备如下三个特点:1. 容易编写;2. 运行速度快;可靠性高。本文正是针对“速度”做出的讨论。

Robotium作为早期Android世界里用得最为广泛的测试框架,基于JUnit扩展了大量关于Android UI的有效测试方法。

Espresso是一个新工具,相对来说,API更加精确,有助于开发者写出更简洁的针对APP的UI测试代码。Espresso的诞生,最大的优势就在于“快”。Robotium的测试代码中,通常会有大量的Sleep,waitFor,以此来等待控件的加载,否则极有可能失败。而Espresso则没有同步的烦恼,因此极大提高了测试速度。

对于大多数基于Android Instrumentation的测试框架(Robotium)来说,测试线程与UI线程是相互独立的,而Espresso则不同,运行时自动与UI线程同步,因此,Espresso的actions和assertions操作运行飞快。

下面,就让我们一起来感受一下“飞一般的感觉“!

 

测试手机:VIVO X520L——CPU:四核;内存:3GB

测试对象:NotePad——Robotium带源码的样例测试应用程序

测试内容:

  1. 新建文本Note 1,保存;
  2. 新建文本Note 2,保存;
  3. 检查Note 1和Note 2新建成功
  4. 点击进入Note 1文本,使用Menu删除;
  5. 长按Note 2进行删除。
  6. 检查Note 1和Note 2删除成功

测试代码

1.    Robotium测试代码

public class NotePadRbot extends ActivityInstrumentationTestCase2<NotesList>{

    private Solo solo;

    public NotePadRbot() {
super(NotesList.class);
} public void testAddNote() throws Exception {
//新建Note 1和Note 2,并判断是否成功
solo.clickOnMenuItem("Add note");
solo.enterText(0, "Note 1");
solo.clickOnMenuItem("Save");
solo.clickOnMenuItem("Add note");
solo.enterText(0, "Note 2");
solo.clickOnMenuItem("Save");
boolean expected = true;
boolean actual = solo.searchText("Note 1") && solo.searchText("Note 2");
assertEquals("Note 1 and/or Note 2 are not found", expected, actual); //删除Note 1和Note 2,并判断是否成功
solo.clickOnText("Note 1");
solo.clickOnMenuItem("Delete");
boolean expected2 = false;
solo.clickLongOnText("Note 2");
solo.clickOnText("Delete");
boolean actual2 = solo.searchText("Note 1") || solo.searchText("Note 2");
assertEquals("Note 1 and/or Note 2 are found", expected2, actual2);
}
}

Robotium测试结果

2.    Espresso测试代码

public void testClickButton() throws InterruptedException {
//添加note1
onView(isRoot()).perform(ViewActions.pressMenuKey());
onView(ViewMatchers.withText("Add note")).perform(ViewActions.click());
onView(ViewMatchers.withId(R.id.note)).perform(ViewActions.typeText("Note 1"),closeSoftKeyboard());
onView(isRoot()).perform(ViewActions.pressMenuKey());
onView(ViewMatchers.withText("Save")).perform(ViewActions.click());
onView(ViewMatchers.withText("Note 1")).check(ViewAssertions.matches(isDisplayed())); //添加note2
onView(isRoot()).perform(ViewActions.pressMenuKey());
onView(ViewMatchers.withText("Add note")).perform(ViewActions.click());
onView(ViewMatchers.withId(R.id.note)).perform(ViewActions.typeText("Note 2"));
onView(isRoot()).perform(ViewActions.pressMenuKey());
onView(ViewMatchers.withText("Save")).perform(ViewActions.click());
onView(ViewMatchers.withText("Note 1")).check(ViewAssertions.matches(isDisplayed())); //菜单删除note1和note2
onView(ViewMatchers.withText("Note 1")).perform(ViewActions.click());
onView(isRoot()).perform(ViewActions.pressMenuKey());
onView(ViewMatchers.withText("Delete")).perform(ViewActions.click());
onView(ViewMatchers.withText("Note 1")).check(ViewAssertions.doesNotExist());
onView(ViewMatchers.withText("Note 2")).perform(ViewActions.longClick());
onView(ViewMatchers.withText("Delete")).perform(ViewActions.click());
onView(ViewMatchers.withText("Note 2")).check(ViewAssertions.doesNotExist());
}

Espresso测试结果

小结

Robotium

Espresso

32.682 s

5.694 s

从表格中,我们可以清晰看到,执行相同的测试用例,Espresso的速度是Robotium的5.7倍。不同于Robotium的sleep/poll机制,Espresso完全受事件驱动,测试线程与UI线程同步,速度优势显著。

最新文章

  1. 如何一步一步用DDD设计一个电商网站(八)—— 会员价的集成
  2. Git系列教程二 基础介绍
  3. 核心动画(CAKeyframeAnimation)
  4. 过河问题nyoj47
  5. Trapping Rain Water
  6. JAVA开发--U盘EXE恢复工具
  7. 使用NPOI完成导出Excel文件
  8. 每天一道LeetCode--118. Pascal&#39;s Triangle(杨辉三角)
  9. 字符串逆转(递归和非递归java)
  10. Python学习笔记012_网络_异常
  11. gcc &amp; gdb &amp; make 定义与区别
  12. 8266编译错误 xtensa-lx106-elf/bin/ld: segmentled section `.text&#39; will not fit in region `iram1_0_seg&#39;
  13. Python-time模块-58
  14. Objective-C 符号化
  15. 常见 Bash 内置变量介绍
  16. 普通01背包问题(dp)
  17. thinkphp中order方法
  18. 13JavaScript运算符
  19. uCOS-ii笔记
  20. 使用navicat进行数据表迁移

热门文章

  1. ElasticSearch(一)概念介绍及环境搭建
  2. 2.pandas的数据结构
  3. Eclipse创建Web项目后新建Servlet时报红叉错误 or 导入别人Web项目时报红叉错误 的解决办法
  4. mysql中的DDL,DML,DQL,DCL
  5. MacOS入门
  6. iOS 高效灵活地配置可复用视图组件的主题
  7. matpltlib 示例
  8. 11-19 configparser模块
  9. PHP asort() 函数
  10. PHP pi() 函数