转一篇老外写的博文:Android automated testing (Robotium)
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:
- Create an Android test project if one does not exist (explained earlier in my post: Android testing in brief).
- 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 { |
- 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.
- 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; |
- Create a test class constructor.
public MyAndroidActivityTest() { |
- 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 |
- 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 { |
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
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)的更多相关文章
- Android application testing with the Android test framework
目录(?)[-] Android automated testing 1 How to test Android applications Tip 2 Unit tests vs functional ...
- 【Android开发日记】之入门篇(十二)——Android组件间的数据传输
组件我们有了,那么我们缺少一个组件之间传递信息的渠道.利用Intent做载体,这是一个王道的做法.还有呢,可以利用文件系统来做数据共享.也可以使用Application设置全局数据,利用组件来进行控制 ...
- csdn肿么了,这两天写的博文都是待审核
昨天早上8点写了一篇博文,然后点击发表,结果系统显示"待审核".于是仅仅好qq联系csdn的客服,等到9点时候,csdn的客服上线了,然后回复说是链接达到5个以上须要审核,于是回到 ...
- 【朝花夕拾】Android性能篇之(六)Android进程管理机制
前言 Android系统与其他操作系统有个很不一样的地方,就是其他操作系统尽可能移除不再活动的进程,从而尽可能保证多的内存空间,而Android系统却是反其道而行之,尽可能保留进程.An ...
- 关于Java多线程的线程同步和线程通信的一些小问题(顺便分享几篇高质量的博文)
Java多线程的线程同步和线程通信的一些小问题(顺便分享几篇质量高的博文) 前言:在学习多线程时,遇到了一些问题,这里我将这些问题都分享出来,同时也分享了几篇其他博客主的博客,并且将我个人的理解也分享 ...
- Flex xml编辑器(老外写的)
github上的一个项目老外写的xml编辑器,灵活利用了Tree的labelFunction实现节点运行时展现.开源地址是 https://github.com/softinsure/XML-Edit ...
- 写一些有关android的东西吧,那时候玩android时候的一些笔记
写一些有关android的东西吧,那时候玩android时候的一些笔记
- 转---秒杀多线程第十四篇 读者写者问题继 读写锁SRWLock
在<秒杀多线程第十一篇读者写者问题>文章中我们使用事件和一个记录读者个数的变量来解决读者写者问题.问题虽然得到了解决,但代码有点复杂.本篇将介绍一种新方法——读写锁SRWLock来解决这一 ...
- 封装一个帮助类来写文件到android外置存储器上
项目地址:点击打开 项目简介:写文件到android外置存储器的一个帮助类,和它的demo程序 它是如何工作的呢? 1.创建 AppExternalFileWriter 对象并传递context(上下 ...
随机推荐
- linux C++ 共享库导出类
1.共享库的对外接口函数的声明必须加上extern “C”. 2.使用共享库对话接口函数生成的对象指针时在该对象未被释放之前不能关闭共享库句柄,否则会出现segmentation fault错误. 以 ...
- 如何在Eclipse中设置默认的JSP文件头部编码
如何在Eclipse中设置默认的JSP文件头部编码 一般,我们为了以后在导入和导出程序的时候(特别是项目较大,文件多)一般都默认文件编码格式为UTF-8 如果你通常都是通过Eclipse来编写程序,那 ...
- 北大poj-1088
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 88484 Accepted: 33177 Description ...
- LeetCode 笔记系列 20 Interleaving String [动态规划的抽象]
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- php大力力 [039节] 修改一下后台项目,同时启用印象笔记,要做的事情todo列表,记录在印象笔记,速度快一些
php大力力 [039节] 修改一下后台项目,同时启用印象笔记,要做的事情todo列表,记录在印象笔记,速度快一些
- 关于设置border的小技巧
可以在需要的时候,在某个元素下面放一个长或宽为1px,或者你需要的border宽度的 div ,再在这个div 上设置border.按需要调整这个div的位置.
- 使用 IMQ+HTB+iptable 统一流量控制心得
IMQ 是中介队列设备的简称,是一个虚拟的网卡设备,与物理网卡不同的是,通过它可以进行全局的流量整形,不需要一个网卡一个网卡地限速.这对有多个ISP接入的情况特别方便.配合 Iptables,可以非常 ...
- Android-->猜拳小游戏
--> 简单的 页面跳转 和 点击事件 的实现... --> AndroidManifest.xml <?xml version="1.0" encoding=& ...
- netfilter-IPv4实现框架分析(一)
基于Linux-2.6.30版本,具体实现net\ipv4\netfilter目录下,入口文件为net\ipv4\netfilter\iptable_filter.c,入口/出口函数为模块的init函 ...
- Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...