使用Espresso测试Android PreferenceFragment

问题描述 投票:4回答:3

我正在尝试在“设置”中为PreferenceFragments片段编写测试。

但是,我一直在收到这个错误:android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: is assignable from class: class android.widget.AdapterView

测试代码如下:

@RunWith(AndroidJUnit4.class)
@SmallTest
public class SettingsFragmentTest {

    @Rule
    public ActivityTestRule<SettingsActivity> mActivityRule = new ActivityTestRule<>(
            SettingsActivity.class);

    @Test
    public void preferredLocationShouldBeVisibleOnDisplay(){
        mActivityRule.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                SettingsFragment settingsFragment = startSettingsFragment();
            }
        });

        // This check passes correctly
        onView(withId(R.id.weather_settings_fragment))
                .check(matches(isCompletelyDisplayed()));

        // This check gives me the NoMatchingViewException
        onData(allOf(is(instanceOf(Preference.class)),
                withKey("location")))
                .check(matches(isCompletelyDisplayed()));
    }

    private SettingsFragment startSettingsFragment(){
        SettingsActivity activity = mActivityRule.getActivity();
        FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
        SettingsFragment settingsFragment = new SettingsFragment();

        transaction.replace(R.id.weather_settings_fragment, settingsFragment, "settingsFragment");
        transaction.commit();

        return settingsFragment;
    }
}

settings_activity布局如下所示:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:name="com.example.android.sunshine.SettingsFragment"
          android:id="@+id/weather_settings_fragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

首选项屏幕布局如下:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">

    <EditTextPreference
        android:defaultValue="@string/pref_location_default"
        android:inputType="text"
        android:key="@string/pref_location_key"
        android:singleLine="true"
        android:title="@string/pref_location_label" />

    <ListPreference
        android:defaultValue="@string/pref_units_metric"
        android:entries="@array/pref_units_options"
        android:entryValues="@array/pref_units_values"
        android:key="@string/pref_units_key"
    android:title="@string/pref_units_label" />

<CheckBoxPreference
    android:defaultValue="@bool/show_notifications_by_default"
    android:key="@string/pref_enable_notifications_key"
    android:summaryOff="@string/pref_enable_notifications_false"
    android:summaryOn="@string/pref_enable_notifications_true"
    android:title="@string/pref_enable_notifications_label" />

</PreferenceScreen>

我无法在线找到有关如何测试PreferenceFragments的任何示例或信息。大多数与测试活动有关的信息。

android testing android-espresso
3个回答
4
投票

PreferenceMatchers似乎只适用于Android框架中的首选项类,但不适用于支持首选项库(com.android.support:preference-v14)。由于后者在内部使用RecylerView,我可以通过使用espresso-contrib中的RecyclerViewActions来获取首选项:

onView(withId(R.id.list))
       .perform(RecyclerViewActions.actionOnItem(hasDescendant(withText(R.string.pref_manage_categories_title)),
            click()));

0
投票

我通常使用Espresso通过他们的活动测试片段。只需测试Fragment公开的UI,就好像它是在ActivityTestRule开始的Activity中一样。如果在活动的初始启动时不存在Fragment,则在测试中导航的方式与用户启动Fragment事务的方式相同。如果你发现自己需要测试需要你在某种测试工具中站起来的业务逻辑,这通常是一个很好的指标,它应该被拉到一个单独的类中,可以(理想情况下)单元测试,没有任何Android依赖。


0
投票

您可以随时尝试Record Espresso test https://developer.android.com/studio/test/espresso-test-recorder.html

以下是如何使用列表https://developer.android.com/training/testing/espresso/lists.html的提示

这是暴露API的PreferenceMatcher,如下所示:

    onData(PreferenceMatchers.withKey(mContext.getString(R.string.key_settings)))
         .perform(click());

0
投票
@Test
    public void clickListPreference() throws Exception{

        // Check if it is displayed
        Context appContext = InstrumentationRegistry.getTargetContext();

        onData(allOf(
           is(instanceOf(Preference.class)),
           withKey(appContext.getResources().getString(R.string.pref_units_key))))
          .check(matches(isDisplayed()));

        // Check if click is working
        onData(allOf(
           is(instanceOf(Preference.class)),
           withKey(appContext.getResources().getString(R.string.pref_units_key))))           
          .onChildView(withText(appContext.getResources()
              .getString(R.string.pref_units_label))).perform(click());
 }

希望对你有帮助..

© www.soinside.com 2019 - 2024. All rights reserved.