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

最新文章

  1. iOS审核这些坑,腾讯游戏也踩过
  2. python学习7
  3. C# 实现 Excel文件的数据导入
  4. python_模块
  5. poj2631 树的直径 + bfs
  6. VHD_Update_diskpart
  7. 一个.java文件中可以有几个同级类
  8. MFC回车事件
  9. MySQL系列教程(一)
  10. (一)linux定时任务的设置 crontab 基础实践
  11. 「LibreOJ β Round #4」框架 [bitset]
  12. Qt: 执行cmd命令;
  13. python小总结4(文件)
  14. SringMVC概述
  15. Dictionary与SortedDictionary
  16. Oracle HAVING子句 - 转
  17. Codeforces 526F Pudding Monsters - CDQ分治 - 桶排序
  18. vue重要特性
  19. System帐户!我使用你登陆
  20. Windows下误删资料的恢复

热门文章

  1. 黑马程序员——OC语言 类和对象
  2. javascript的异步编程方法
  3. 通过j-interop访问WMI实例代码
  4. HDU 2089 不要62
  5. 12-4mysql 查询
  6. js 设置Cookie 在PHP中读取(大师手法)
  7. nat转换
  8. Spark 个人实战系列(1)--Spark 集群安装
  9. Linux命令--top使用技巧
  10. vim编辑器使用相关