Robotium的中文资料甚少,只得求助于老外,发现了一篇不错的文章:https://blog.codecentric.de/en/2011/03/android-automated-testing-robotium/

但是不知道是不是公司网络问题,codecentric网站打开超慢,故转Mihal Celovski的博文到这里备用,向原作者致敬!

开始正文:

In the previous GWT project we worked with acceptance tests and the Robot Framework. The question I asked myself was if something similar exists for Android. Yes, it exists, and its name is Robotium.
Robotium is a test framework created to write robust automatic black-box test cases for Android applications. With the support of Robotium,
test case developers can write function, system and acceptance test
scenarios, spanning multiple Android activities. It allows you to create
test cases for Android Activities, Dialogs, Toasts, Menus and Context
Menus etc.

This is one very simple example how to write Robotium tests:

    1. Create an Android test project if one does not exist (explained earlier in my post: Android testing in brief).
    2. Create a test class. For example, if you want to test activity
      “MyAndroidActivity”, your test class then needs to extend
      android.test.ActivityInstrumentationTestCase2.
public class MyAndroidActivityTest extends android.test.ActivityInstrumentationTestCase2 {
    1. Download robotium-solo-xxx.jar library or add a dependency to it in the *.pom file of your maven project. It’s open source at Google Code.
    2. Declare a Solo class instance. When writing tests there is no need to plan for or expect new activities in the test case. All is handled automatically by Robotium-Solo. Robotium-Solo can be used in conjunction with ActivityInstrumentationTestCase2. The test cases are written from a user perspective were technical details are not needed.
private Solo solo;
    1. Create a test class constructor.
public MyAndroidActivityTest() {
super("com.example", MyAndroidActivity.class);
}
    1. Override the setUp() and tearDown() methods. Overriden  setUp() method is usually the  place where you  instantiate Solo object. In teardDown()  method solo.finalize()  is called and all activites that have been opened are finished.
@Override
protected void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
 
@Override
protected void tearDown() throws Exception {
try {
solo.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}
    1. Create a test method. Robotium-Solo offers a number of methods for testing various UI events such as: clickOnText(), enterText(), clearEditText, goBack, serachText() etc.
public void testDisplayedText() throws InterruptedException {
Assert.assertTrue(solo.searchText("Hello world,my activity"));
Assert.assertTrue(getActivity().getString(R.string.hello_string).equals(solo.getCurrentActivity().getTitle()));
}

As you can see it is all very similar to Robot-Selenium tests. When you decide to run this test you have to start the Emulator or connect an actual device to your computer. Then you just select your test class and select Run As/ Android JUnit test. After that you can see your application start and some actions execute. The JUnit view in your Eclipse window shows the current progress and success of the test runs.

Robotium benefits

Robotium tests have great readability compared to standard instrumentation tests. No great knowledge of the tested application is needed, just things like the name of the Activity, displayed texts or anything related to application’s UI. But one of the greatest benefits for us is the possibility to integrate tests with Maven as part of continuous integration.

更新部分博主的问答:

Shivang Seth says:

Would you elaborate on how to write test cases for an application having multiple activities/services. Should we extend the test case classes from android.test.ActivityInstrumentationTestCase2 or android.test.InstrumentationTestCase? An example would be very handy. Thank you

Mihal Celovski says:
You can test a number of activities by using solo.goBack () method and going forward and backward through activities. Example:

solo.clickOnButton(“Start Activity1″); // start the first activity
Assert.assertTrue(solo.searchText(“Some text on Activity1″));
Assert.assertTrue(getActivity().getString(R.string.activity1_title)
.equals(solo.getCurrentActivity().getTitle()));

solo.goBack(); // go back to start page(activity)

solo.clickOnButton(“Start Activity2″); // start the second activity

转一篇老外写的博文:Android automated testing (Robotium)的更多相关文章

  1. Android application testing with the Android test framework

    目录(?)[-] Android automated testing 1 How to test Android applications Tip 2 Unit tests vs functional ...

  2. 【Android开发日记】之入门篇(十二)——Android组件间的数据传输

    组件我们有了,那么我们缺少一个组件之间传递信息的渠道.利用Intent做载体,这是一个王道的做法.还有呢,可以利用文件系统来做数据共享.也可以使用Application设置全局数据,利用组件来进行控制 ...

  3. csdn肿么了,这两天写的博文都是待审核

    昨天早上8点写了一篇博文,然后点击发表,结果系统显示"待审核".于是仅仅好qq联系csdn的客服,等到9点时候,csdn的客服上线了,然后回复说是链接达到5个以上须要审核,于是回到 ...

  4. 【朝花夕拾】Android性能篇之(六)Android进程管理机制

    前言        Android系统与其他操作系统有个很不一样的地方,就是其他操作系统尽可能移除不再活动的进程,从而尽可能保证多的内存空间,而Android系统却是反其道而行之,尽可能保留进程.An ...

  5. 关于Java多线程的线程同步和线程通信的一些小问题(顺便分享几篇高质量的博文)

    Java多线程的线程同步和线程通信的一些小问题(顺便分享几篇质量高的博文) 前言:在学习多线程时,遇到了一些问题,这里我将这些问题都分享出来,同时也分享了几篇其他博客主的博客,并且将我个人的理解也分享 ...

  6. Flex xml编辑器(老外写的)

    github上的一个项目老外写的xml编辑器,灵活利用了Tree的labelFunction实现节点运行时展现.开源地址是 https://github.com/softinsure/XML-Edit ...

  7. 写一些有关android的东西吧,那时候玩android时候的一些笔记

    写一些有关android的东西吧,那时候玩android时候的一些笔记

  8. 转---秒杀多线程第十四篇 读者写者问题继 读写锁SRWLock

    在<秒杀多线程第十一篇读者写者问题>文章中我们使用事件和一个记录读者个数的变量来解决读者写者问题.问题虽然得到了解决,但代码有点复杂.本篇将介绍一种新方法——读写锁SRWLock来解决这一 ...

  9. 封装一个帮助类来写文件到android外置存储器上

    项目地址:点击打开 项目简介:写文件到android外置存储器的一个帮助类,和它的demo程序 它是如何工作的呢? 1.创建 AppExternalFileWriter 对象并传递context(上下 ...

随机推荐

  1. Annotation注解(有源代码)

    注解(annotation)概述: ·从JDK5.0 开始,java增加了对元数据(MetaData)的支持,也就是Annotation(注解) ·Annotation其实就是代码里的特殊标记,这些标 ...

  2. Bean property '**DAO' is not writable or has an invalid setter method

    ApplicationContext.xml中配置有问题,里面的bean的属性名称写错了. ApplicationContext.xml中写的是loginDAO,在java类里配置的bean却写成了l ...

  3. Linq二 LinqToSql

    虽然微软已经停止更新了LinqToSql,但是目前的已完全满足目前的需求. 第一步:添加LinqToSql 第二步:将其关联的Sqlserver数据库 第三步:数据库已变成实体类 第四步:可以对数据库 ...

  4. 在MacOX下安装python-opencv

    下载好opencv之后 1. 在文件夹下新建一个release或build的文件夹: 2. cmake . make 3.在该build文件夹下 nano .bash_profile 把python的 ...

  5. 打印datagridview内容 实现横向纵向分页(转)

    网上找了很多打印的,只发现这个比较好,实现了横向纵向分页. 代码如下: using System;using System.Collections.Generic;using System.Text; ...

  6. Makefile 开发环境全能管家

    变量的应用: CC=gcc RM=rm EXE=main.exe OBJS=目标 伪目标的应用: .PHONY:clean 自动变量的应用: $@:表示一个规则的目标 $^:表示的是规则中的所有的先决 ...

  7. HC系列蓝牙模块连接单片机与电脑,传输数据(蓝牙心电测试)

    毕设做无线心电监护.有线的做出来了,AD8232+MCU+LabVIEW上位机.pcb还没时间搞,这个9*7*2.5cm拿来测试能用. 自己做了AD8232的模拟前端,打的板子还没到没法测试. 虽然比 ...

  8. CentOS 6.5设置静态IP教程 并且可以ping通

    CentOS6.5掉电或重启,它的IP会被DHCP重新分配,如果要远程控制这台电脑,不得不去打开显示器去查看它的新IP,这样太麻烦了.于是需要将这台电脑的IP设置成静态的. 网上常规的设置静态ip的方 ...

  9. yii使用MongoDB作为数据库服务软件[win7环境下](1)

    1.进入http://php.net,在站内搜索栏搜索mongodb,查看相关的安装步骤信息. 2.找到相应的php.ini配置文件,使用wampserver等服务器软件时,千万不要找错了php.in ...

  10. jQuery 获取父窗口的元素 父窗口 子窗口(iframe)

    $("#父窗口元素ID",window.parent.document); 对应javascript版本为window.parent.document.getElementById ...