生命周期测试与Robotium:杀戮和重新启动活动 [英] Lifecycle Testing with Robotium: Killing and Restarting Activity

查看:179
本文介绍了生命周期测试与Robotium:杀戮和重新启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何是否有可能重新启动,使用结束活动Robotium的 solo.goBack()

How is it possible to restart an activity that was ended using Robotium's solo.goBack()?

以下不重新启动的活动:(测试完成后确定)

The following does not restart the activity: (the test finishes ok)

    solo.goBack();
    try {
    //  recreate activity here
    runTestOnUiThread(new Runnable() {
        public void run() {
        getInstrumentation().callActivityOnCreate(getActivity(), 
                              null);
        getInstrumentation().callActivityOnStart(getActivity());
        getInstrumentation().callActivityOnResume(getActivity());
        }});
    }

你怎么重新启动,是由 Solo.goBack结束的活动()

  • 测试活动与robotium 流 地址Robotium两项活动的变化,而不是destoying并重新启动。
  • <一个href="http://stackoverflow.com/questions/29435456/simulate-android-killing-and-restart-service">Simulate Android的杀戮和重新启动服务 与服务,而不是活动的交易(且没有答案)
  • <一个href="http://stackoverflow.com/questions/14608353/activity-doesnt-restart-in-different-tests-with-robotium">Activity在不同的测试与Robotium不重新启动 问如何手动重新启动该活动,但以不同的方式回答
  • Testing activity flow with robotium addresses changing between two activities in Robotium, not destoying and restarting.
  • Simulate Android killing and restart service deals with a service, not an activity (and is unanswered)
  • Activity doesn't restart in different tests with Robotium asks how to restart the activity manually, but is answered in a different way

要重现一个最小的测试是这样,创建一个项目,其测试项目:

To reproduce a minimal test like this, create a project and its test project:

android create project -t 1 -p testRestart -k com.testRestart -a testactivity
cd testRestart
mkdir tests
cd tests
android create test-project -m .. -p .

复制 Robotium罐子测试/库文件夹。

package com.testRestart;

import android.test.ActivityInstrumentationTestCase2;
import com.robotium.solo.Solo;

public class testactivityTest extends ActivityInstrumentationTestCase2<testactivity> {
    private Solo solo;

    protected void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
    }
    public void tearDown() throws Exception {
    solo.finishOpenedActivities();
    }

    public testactivityTest() {
        super("com.testRestart", testactivity.class);

    }

    public void testDestroyAndRestart() {
        solo.goBack();
    try {
        //  recreate activity here
        runTestOnUiThread(new Runnable() {
            public void run() {
            getInstrumentation().callActivityOnCreate(getActivity(), 
                                  null);
            getInstrumentation().callActivityOnStart(getActivity());
            getInstrumentation().callActivityOnResume(getActivity());
            }});
    } catch ( Throwable t ) {
        throw new RuntimeException(t);
    }
    }
}

在测试文件夹中,做了

ant debug install
adb shell am instrument -w -e class com.testRestart.testactivityTest com.testRestart.tests/android.test.InstrumentationTestRunner

问题又来了:你怎么能重新启动,是由 Solo.goBack()结束活动

推荐答案

由于@IHeartAndroid在他的回答以<一个href="http://stackoverflow.com/questions/11644288/how-to-relaunch-the-closed-app-in-robotium/17744154#17744154">this robotium问题(我还没有看到它之前,有通过@Flavio卡帕乔在注释为<一个链接href="http://stackoverflow.com/questions/13802233/how-to-restart-killed-android-application-in-testing-with-robotium?rq=1">related问题):

As @IHeartAndroid said in his answer to this robotium question (I had not seen it before, there was a link by @Flavio Capaccio in a comment to a "related question"):

launchActivity("com.testRestart", testactivity.class, null);

工作。这是 InstrumentationTestCase 的功能。

(如果你想upvote这个答案,upvote 他的回答为好)

(If you want to upvote this answer, upvote his answer as well)

这篇关于生命周期测试与Robotium:杀戮和重新启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆