Android测试:ActivityInstrumentationTestCase2:测试2按钮,其中每个按钮导航到另一个活动

时间:2014-04-14 13:06:10

标签: android unit-testing android-testing

我正在尝试为我的应用程序进行一些Android测试。 我有一个活动(A),其中包含两个按钮,每个按钮导航到另一个活动(B),意图中有不同的额外(数据)。

我可以测试其中一个,但不能同时测试两个。因为当我执行第一次点击时,我导航到下一个活动(B),第二个按钮不再可见。

我的问题是: 1-是否可以在同一个活动中进行多次测试? 2-是否有一种好方法或最佳实践来创建许多用于测试的案例场景? 示例--->就像我点击第一个按钮,我导航到Activity(B),然后再次重新启动Activity(A),然后单击第二个按钮?

无论如何,谢谢。

这是Activitytest的代码:

public class MyActivityTest extends
ActivityInstrumentationTestCase2<MyActivity> {

    /**
    * first button.
    */
    View aButton;

    /**
    * second button.
    */
    View bButton;

    /**
    * Activity under test.
    */
    MyActivity activity;

    /**
    * Create a new MyActivityTest.
    */
    public MyActivityTest() {
        super(MyActivity.class);
    }

    /*
    * (non-Javadoc)
    *
    * @see android.test.ActivityInstrumentationTestCase2#setUp()
    */
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        activity = getActivity();
    }

    /**
    * Tests the behavior when the user selects first button.
    */
    @MediumTest
    public void testClickFirstButton() {

        // ensure a valid handle to the activity has been returned
        assertNotNull(activity);

        aButton = (View) activity
        .findViewById(MyPackage.R.id.first_button);
        assertNotNull(aButton);

        activity.runOnUiThread(new Runnable() {
            public void run() {
                aButton.performClick();
            }
        });

        // wait for the request to go through
        getInstrumentation().waitForIdleSync();
    }

    /**
    * ========================================> This test is not running
    * Tests the behavior when the user selects second button.
    */
    @MediumTest
    public void testClickSecondButton() {

        // ensure a valid handle to the activity has been returned
        assertNotNull(activity);

        bButton = (View) activity
        .findViewById(MyPackage.R.id.second_button);
        assertNotNull(bButton);

        activity.runOnUiThread(new Runnable() {
            public void run() {
                bButton.performClick();
            }
        });

        // wait for the request to go through
        getInstrumentation().waitForIdleSync();

    }
}

1 个答案:

答案 0 :(得分:2)

我找到了我的解决方案,但如果有人有更好的解决方案,欢迎:

我通过覆盖tearDown()更新代码,并通过使用Solo实例,通过关闭所有打开的活动从一个案例转移到另一个案例,所以我也更新了setUp()方法

@Override
protected void setUp() throws Exception {
    activity = getActivity();
    solo = new Solo(getInstrumentation(), getActivity());
}

@Override
public void tearDown() throws Exception {
    // tearDown() is run after a test case has finished.
    // finishOpenedActivities() will finish all the activities that have
    // been opened during the test execution.
    solo.finishOpenedActivities();
    super.tearDown();
}

所以现在我的测试用例一个接一个地运行。每个案例都会关闭并重新启动活动。

独自来自哪里?

您需要添加Robotium依赖项:

dependencies {
   // Unit testing dependencies
   androidTestCompile 'com.jayway.android.robotium:robotium:5.4.12'
} 

您可以查看this tutorial