Android:Robotium v​​s android测试框架

时间:2012-11-04 19:00:34

标签: android robotium ui-testing

每个人都使用Robotium进行GUI测试。

你能告诉我Robotium可以做什么Android原生测试框架吗? 据我所知,Robotium可以用作黑盒测试,所以我不需要了解应用程序资源。 还有什么?

3 个答案:

答案 0 :(得分:17)

Robotium

<强> 优点:

  • 使用webViews支持混合应用程序。
  • 您可以将测试绑定到测试应用程序的唯一ID,只要使用UIatomator编写复杂的测试用例就需要大量的工作
  • UIatomator中的
  • 使用的是UiAutomatorTestCase Instrumentation,它不会为您提供调用当前活动的可能性并检查加载的适当活动,您无法调用连接或音频管理器进行Wi-Fi或声音测试。在Robotium中,您可以轻松调用此类宝石,这可以极大地提高测试效率
  • 开源项目,因此您可以根据需要修改工具

<强> 缺点:

  • 无法测试多android:process标记用于不同活动的多进程应用程序。

示例代码:

Robotium API example

UIautomator

<强> 优点:

  • 测试用例独立于测试应用程序的工作过程。因此,它可以在使用其他库的地方使用,或者在其他过程中运行活动也很有用,如果需要在多个应用程序之间切换测试。

<强> 缺点:

  • 仅适用于Android 4.1或更高版本!

  • 获取ui-object实例时无法使用源ID。这意味着如果在一个布局上更改了应用程序结构,则需要重构测试(可以通过为每个ui元素使用标记来解决此问题)

  • 您无法获取当前活动或工具。这意味着你在测试开发方面受到限制,并且没有使用许多android的api方法进行测试。

  • 难以调试,您需要有快速建立和开始测试的脚本并查看输出

层次结构查看器:

Tool for working with UIautomator

我认为这两个工具都很好,绝对可以在测试期间在应用程序之间切换是很棒的。您应该根据需要选择工具。我建议Robotium进行测试,你不需要在应用程序之间切换,因为它有简单的方法和机会使用Android API在短代码中编写灵活和智能的测试,甚至可以覆盖webview和milti-process中的网页测试申请非常不寻常。

答案 1 :(得分:7)

Robotium和本机工具之间的区别在于,使用Robotium编写测试非常简单。它基本上是从Solo实例对象中指向并单击。

Here您可以下载JAR文件和示例项目来自行测试。

<强>更新

作为一个例子,我正在测试一个Activity,其中包含一些Edit Text,一个微调器和一个弹出对话框,当我点击一个微调器选项时它会显示出来。请注意,使用其他方法,填充弹出对话框的字段真是太痛苦了。

以下是如何声明测试类和Robotium的初始化

import com.jayway.android.robotium.solo.Solo; //Using Robotium

//Robotium uses ActivityInstrumentationTestCase2.
//Note here the use of the template
public class AddNewQuestionTests extends
                ActivityInstrumentationTestCase2<AddNewQuestion> {

    public AddNewQuestionTests(Class<AddNewQuestion> name) {
        super(name);
    }

    public AddNewQuestionTests() {
        super(AddNewQuestion.class);
    }

    private Solo solo;

    protected void setUp() throws Exception {
        super.setUp();
//Initialize Solo with the instrumentation and the activity under test
        solo = new Solo(getInstrumentation(), getActivity());       
    }

以下是测试方法:

    public void testHappyPathAddScaleQuestion() {

            // Type question title
            solo.clickOnEditText(0); //find the EditText, and click it

            solo.enterText((EditText) getActivity().findViewById(

//find the EditText, and put some string
                R.id.activity_add_new_question_editText_question_title),
                            "Question title scale ");
            // Type question description
            solo.clickOnEditText(1);
            solo.enterText((EditText) getActivity().findViewById(
                            R.id.activity_add_new_question_editText_question_description),
                            "Question description scale");
            // Type the question
            solo.clickOnEditText(2);
            solo.enterText((EditText) getActivity().findViewById(
                            R.id.activity_add_new_question_editText_question),
                            "Question scale");

            // Click the spinner and then the "Scale" question type
//Press an spinner option
            solo.pressSpinnerItem(0, 4);

//Wait for the popup dialog title to show up. When robotium reads it, continue working          solo.waitForText(getActivity().getResources().getString(R.string.activity_add_new_question_scale_selection_dialog_message)); 
            // Type minimum and maximum ranges
            solo.clickOnEditText(0);
            solo.searchText(getActivity().getResources().getString(R.string.activity_add_new_question_maximum_value_hint));
            solo.clickOnView(solo.getCurrentEditTexts().get(0));
            solo.enterText(0, "34");
            solo.clickOnView(solo.getCurrentEditTexts().get(0));
            solo.enterText(1, "55");

            // Click ok to close the dialog
            solo.clickOnButton(getActivity().getResources().getString(R.string.OK));

            // Click ok to get an ok message
            solo.clickOnButton(getActivity().getResources().getString(R.string.OK));

            //Wait for the ok toast message
            boolean flagOKDatabase=solo.waitForText(getActivity().getResources().getString(R.string.database_success_storing_data),1,120);
            assertEquals("Something wrong happened with the database", true, flagOKDatabase);
        }

答案 2 :(得分:3)

在android本机的工具框架上使用robotium没有任何缺点。这是因为当使用robotium时,如果没有它,你仍然可以做任何你能做的事情,但你也可以访问许多有用的功能。

还有其他Android自动化框架,但绝对值得一看。如果您在Web视图中有任何代码,这些代码尤其有用,因为这是robotium真正让自己失望的地方。

https://github.com/calabash/calabash-android

https://github.com/calabash-driver/calabash-driver

http://testdroid.com/