Robotium和Espresso大PK——速度篇

引言

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线程同步,速度优势显著。

原文地址:https://www.cnblogs.com/renzhuo/p/EspressoVsRobotium4Rate.html