1

Android の自動化に Robotium を使用すると、テスト ケースごとに同じ手順を作成していることに気づきます。

常に「ログイン」と「ログアウト」が必要です。rLogin(); を呼び出すだけで、FunctionsTestClass を作成しようとしてきました。および rLogout();

次に例を示します。

完全なファイルを追加します。

'package com.myproject.mobile.test;

import android.test.ActivityInstrumentationTestCase2;
import android.app.Activity;
import junit.framework.AssertionFailedError;

import com.bitbar.recorder.extensions.ExtSolo;
import com.jayway.android.robotium.solo.By;

import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

'public class Logout extends ActivityInstrumentationTestCase2<Activity> {

private static final String LAUNCHER_ACTIVITY_CLASSNAME = "com.myproject.mobile.MainActivity";
private static Class<?> launchActivityClass;
static {
    try {
        launchActivityClass = Class.forName(LAUNCHER_ACTIVITY_CLASSNAME);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}
private static ExtSolo solo; // ExtSolo is an extension of Robotium Solo that helps
                        // collecting better test execution data during test
                        // runs

@SuppressWarnings("unchecked")
public Logout() {
    super((Class<Activity>) launchActivityClass);
}

@Override
public void setUp() throws Exception {
    super.setUp();
    solo = new ExtSolo(getInstrumentation(), getActivity(), this.getClass()
            .getCanonicalName(), getName());
}

@Override
public void tearDown() throws Exception {
    solo.finishOpenedActivities();
    solo.tearDown();
    super.tearDown();
}


public static void logginin() throws Exception {
    try {
    //enter username
        solo.sleep(17000);

        throw e;
    } catch (Exception e) {
        solo.fail(
                "com.myproject.mobile.test.MainActivityTest.testRecorded_scr_fail",
                e);
        throw e;
    }
}

}'

2 番目のファイルを追加する

package com.mypackage.mobile.test;

import android.test.ActivityInstrumentationTestCase2;
import android.app.Activity;
import junit.framework.AssertionFailedError;

import com.bitbar.recorder.extensions.ExtSolo;
import com.jayway.android.robotium.solo.By;
import com.mypackage.mobile.test.*;


import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

'public class Test extends ActivityInstrumentationTestCase2<Activity> {

private static final String LAUNCHER_ACTIVITY_CLASSNAME =     "com.mypackage.mobile.MainActivity";
private static Class<?> launchActivityClass;

static {
    try {
        launchActivityClass = Class.forName(LAUNCHER_ACTIVITY_CLASSNAME);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}
private static ExtSolo solo; // ExtSolo is an extension of Robotium Solo that helps
                        // collecting better test execution data during test
                        // runs

@SuppressWarnings("unchecked")
public Test() {
    super((Class<Activity>) launchActivityClass);
}

@Override
public void setUp() throws Exception {
    super.setUp();
    solo = new ExtSolo(getInstrumentation(), getActivity(), this.getClass()
            .getCanonicalName(), getName());
}

@Override
public void tearDown() throws Exception {
    solo.finishOpenedActivities();
    solo.tearDown();
    super.tearDown();
}



public void testRecorded() throws Exception {
    try {

        Logout.logginin();


    } catch (AssertionFailedError e) {
        solo.fail(
                "com.mypackage.name.MainActivityTest.testRecorded_scr_fail",
                e);
        throw e;
    } catch (Exception e) {
        solo.fail(
                            "com.mypackage.name.MainActivityTest.testRecorded_scr_fail",
                e);
        throw e;
    }
}

    }

私のプロジェクトを反映するために、下の 2 つのコードを更新しました。

4

1 に答える 1

1

name でテストケースを作成しないでくださいtestLoggingin()。その代わりに、functionを持つクラスを作成します。そのため、必要なときにいつでもインポートして、関数を呼び出してログインできます。また、 を使用して条件を確認できます。 login()assert

于 2013-08-07T06:31:28.753 に答える