【问题标题】:Android Test : ActivityInstrumentationTestCase2 : Test 2 buttons, where each one navigates to another activityAndroid 测试:ActivityInstrumentationTestCase2:测试 2 个按钮,每个按钮都导航到另一个活动
【发布时间】:2014-04-14 13:06:09
【问题描述】:

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

我可以测试其中一个,但不能同时测试两者。因为当我执行第一个的点击时,我导航到下一个 Activity (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();

    }
}

【问题讨论】:

    标签: android unit-testing android-testing


    【解决方案1】:

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

    我通过覆盖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();
    }
    

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

    solo 来自哪里?

    你需要添加Robotium依赖:

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

    你可以去this tutorial看看。

    【讨论】:

    • 抱歉,Solo 类型到底是什么?
    • 我更新了我的答案。您需要添加robotium 库才能执行此操作。
    • 嗯,有道理:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 2020-10-14
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    相关资源
    最近更新 更多