web-dev-qa-db-ja.com

Espressoで特定のアクティビティをテストする方法は?

私はAndroidでのテストから始めたばかりですが、これは非常に基本的なようですが、グーグルで調べても、どこにも答えが見つかりません。

私のAndroidアプリでは、表示される最初のアクティビティはログイン画面で、その後にホーム画面があり、他のアクティビティに移動するためのオプションがあります。アクティビティをテストするために、現在移動する必要があります最初にこれらの2つの活動を通じて。

Espressoテストを(ActvityTestRule/JUnit4を使用して)セットアップして、すぐにテストするアクティビティを起動するようにするにはどうすればよいですか?

[〜#〜]編集[〜#〜]

より具体的には、私の問題は、Espressoテストで見たすべてのチュートリアルで、すべてのテストが次のようなActivityTestRuleを使用してアプリケーションのメインアクティビティから始まることです。

@Rule
public final ActivityTestRule<MainActivity> mRule = new ActivityTestRule<MainActivity>(MainActivity.class);

指定したアクティビティでテストを開始したいのですが、現在、このようなテストコードを繰り返し繰り返すことでナビゲートしています。

onView(withId(R.id.go_to_other_activity_button)).perform(click())
15
Moses

特定のアクティビティを実行する場合:ActivityTestRuleまたはIntentTestRuleが必要です。

契約はかなり自明であり、最初のパラメーターとして開始する必要なアクティビティを設定するだけです。

ActivityTestRule<>(ActivityYouWantToStart.class, initialTouchMode, launchActivity) //rule for activity start


IntentTestRule<>(ActivityYouWantToStart.class, initialTouchMode, launchActivity) //used for testing intents and activities

ここで注目すべき重要なのはlaunchActivityブールです。これをtrueに設定すると、各テストが自動的にアクティビティを起動します。

その他の使用方法としては、一部のアクティビティが実行前に何らかの意図、データなどが送信されることを想定している場合、このパラメーターをfalseに設定し、テスト/アクティビティを実行する前にアプリを準備できます。

たとえば、アクティビティBを実行するために共有設定に保存されたログインユーザーが必要で、そのアクティビティのロジックは次のようになっているとします。

if(!userIsLoggedIn()){

    jumpToMainActivity();

}

次に、そのテストを実行する前にモックユーザーを保存したり、たとえばデータベースにモックオブジェクトを入力したり、アクティビティBの環境を準備した後にすべてを呼び出したりできます。

mActivityRule.launchActivity(null);

LaunchActivityがパラメーターをとることに注意してください。これは、実際にアクティビティBが受け取るインテントです。テストを開始する前にインテントを準備できるような方法で追加のデータが必要な場合。

カスタムアクティビティを実行する完全な例:

/**
* Testing of the snackbar activity.
**/
@RunWith(AndroidJUnit4.class)
@LargeTest
public class SnackbarActivityTest{
    //espresso rule which tells which activity to start
    @Rule
    public final ActivityTestRule<SnackbarActivity> mActivityRule = 
        new ActivityTestRule<>(SnackbarActivity.class, true, false);


    @Override
    public void tearDown() throws Exception {
        super.tearDown();
        //just an example how tear down should cleanup after itself
        mDatabase.clear();
        mSharedPrefs.clear();
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        //setting up your application, for example if you need to have a user in shared
        //preferences to stay logged in you can do that for all tests in your setup
        User mUser = new User();
        mUser.setToken("randomToken");
    }

    /**
    *Test methods should always start with "testXYZ" and it is a good idea to 
    *name them after the intent what you want to test
    **/
    @Test
    public void testSnackbarIsShown() {
        //start our activity
        mActivityRule.launchActivity(null);
        //check is our text entry displayed and enter some text to it
        String textToType="new snackbar text";
        onView(withId(R.id.textEntry)).check(matches(isDisplayed()));
        onView(withId(R.id.textEntry)).perform(typeText(textToType));
        //click the button to show the snackbar
        onView(withId(R.id.shownSnackbarBtn)).perform(click());
        //assert that a view with snackbar_id with text which we typed and is displayed
        onView(allOf(withId(Android.support.design.R.id.snackbar_text), 
        withText(textToType))) .check(matches(isDisplayed()));
    }
}
12
originx