如何使用Espresso测试片段

gre*_*obo 13 android android-espresso

我有一个我要测试的Android片段.我创建了一个测试活动,我添加了这个片段并运行了一些Espresso测试.

但是,Espresso在片段中找不到任何视图.它转储视图层次结构,它全部为空.

我不想使用实际的父活动.我想单独测试这个片段.有没有人这样做过?是否有类似代码的示例?

@RunWith(AndroidJUnit4.class)
class MyFragmentTest {
    @Rule
    public ActivityTestRule activityRule = new ActivityTestRule<>(
    TestActivity.class);

    @Test
    public void testView() {
       MyFragment myFragment = startMyFragment();
       myFragment.onEvent(new MyEvent());
       // MyFragment has a recyclerview. 
       //OnEvent is EventBus callback that in this test contains no data.
       //I want the fragment to display empty list text and hide the recyclerView
       onView(withId(R.id.my_empty_text)).check(matches(isDisplayed()));
       onView(withId(R.id.my_recycler)).check(doesNotExist()));
    }

    private MyFragment startMyFragment() {
         FragmentActivity activity = (FragmentActivity) activityRule.getActivity();
    FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
    MyFragment myFragment = new MyFragment();
    transaction.add(myFragment, "myfrag");
    transaction.commit();
    return myFragment;
    }
}
Run Code Online (Sandbox Code Playgroud)

Ank*_*kur 6

我将按照以下方式创建一个 ViewAction 如下:

public static ViewAction doTaskInUIThread(final Runnable r) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isRoot();
        }

        @Override
        public String getDescription() {
            return null;
        }

        @Override
        public void perform(UiController uiController, View view) {
            r.run();
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

然后使用下面来启动应该在 UI 线程中运行的代码

onView(isRoot()).perform(doTaskInUIThread(new Runnable() {
        @Override
        public void run() {
            //Code to add your fragment or anytask that you want to do from UI Thread
        }
    }));
Run Code Online (Sandbox Code Playgroud)

下面是添加片段视图层次结构的测试用例示例

    @Test
public void testSelectionOfTagsAndOpenOtherPage() throws Exception{

    Runnable r = new Runnable() {
        @Override
        public void run() {
            //Task that need to be done in UI Thread (below I am adding a fragment)

        }
    };
    onView(isRoot()).perform(doTaskInUIThread(r));

}
Run Code Online (Sandbox Code Playgroud)


小智 5

public class VoiceFullScreenTest {
    @Rule
    public ActivityTestRule activityRule = new ActivityTestRule<>(
            TestActivity.class);

    @Test
    public void fragment_can_be_instantiated() {
        activityRule.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                VoiceFragment voiceFragment = startVoiceFragment();
            }
        });
        // Then use Espresso to test the Fragment
        onView(withId(R.id.iv_record_image)).check(matches(isDisplayed()));
    }

    private VoiceFragment startVoiceFragment() {
        TestActivity activity = (TestActivity) activityRule.getActivity();
        FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
        VoiceFragment voiceFragment = new VoiceFragment();
        transaction.add(voiceFragment, "voiceFragment");
        transaction.commit();
        return voiceFragment;
    }


}
Run Code Online (Sandbox Code Playgroud)

如上所述,您可以从 UI 线程开始您的片段。


Adi*_*ain 5

您可以使用androidx.fragment:fragment-testing图书馆。在测试方法中启动片段非常简单:

val fragmentArgs = Bundle()
androidx.fragment.app.testing.launchFragmentInContainer<MyFragment>(fragmentArgs)
Run Code Online (Sandbox Code Playgroud)

您可以在测试您的片段Android 开发人员指南中找到有关此库的更多信息。


kar*_*oss 1

您可能忘记将片段注入视图层次结构中。尝试在布局中为片段定义持有容器TestActivity(例如FrameLayout带有 id 的fragment_container容器),然后add(myFragment, "tag")使用add(R.id.fragment_container, myFragment, "tag")此方法)而不是仅仅使用。replace我想您也可以使用具有相同签名的方法。