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. ConvertFrom-String 命令研究

    -------先上个例子------- $aaa = @'0.027 0.034 0.834 0.1050.346 0.558 0.018 0.0780.001 0.997 0.001 0.0010. ...

  2. ubuntu安装无线驱动

    一.无线驱动的安装. 首先查看自己的网卡类型:lspci -vnn | grep 14e4 查看内核版本: -uname -r 去www.broadcom.com/support/802.11/lin ...

  3. iOS 7 UITableview 在Plain模式下 设置背景颜色无效

    在iOS6的时候,设置Plain模式下table的颜色 通过[self.table setBackgroundColor:[UIColor red]]; 就可以看到一个满身通红的tableView 但 ...

  4. jQuery.ajax() 函数详解

    jQuery.ajax()函数用于通过后台HTTP请求加载远程数据. jQuery.ajax()函数是jQuery封装的AJAX技术实现,通过该函数,我们无需刷新当前页面即可获取远程服务器上的数据. ...

  5. hdu 2067

    ps:晕,for()是先判断,再执行的...WA了一次...这个也是递推.第一列只有从上面来的点,所以全部是1(dp[i][0]=1) 其他的可以从上面或者左边来所以是 dp[i][j]=dp[i-1 ...

  6. SVD小结

    1.矩阵分解 假设一个矩阵Data是m行n列,SVD(奇异值分解)将Data分解为U,E,VT 三个矩阵: Datam*n=Um*kEk*kVTk*n E是一个对角矩阵,对角元素为奇异值,对应Data ...

  7. PHP flush sleep 输出缓存控制详解

    1 2 3 4 5 6 ob_start,flush,ob_flush for($i=0;$i<</SPAN>10;$i++) { echo $i.''; flush(); slee ...

  8. ACE - Reactor模式源码剖析及具体实现(大量源码慎入)

    原文出自http://www.cnblogs.com/binchen-china,禁止转载. 在之前的文章中提到过Reactor模式和Preactor模式,现在利用ACE的Reactor来实现一个基于 ...

  9. Unity3D 发布无边框exe

    关于:Unity3D 发布无边框exe,Unity3D Build exe无边框 Unity发布windows版本 总是带着边框,很想给它去掉,笔者在网上查了一番,常见的有3中. 1:通过unity3 ...

  10. crontab不能正确执行的问题

    近期在部署crontab任务的时候,总是遇到在shell中单独执行正常,但是放到crontab定时执行出错的问题.若出现这类场景,九成就是环境变量的问题. 因为我的定制任务,基本上都需要使用sqlpl ...