如何使用Espresso测试片段

时间:2016-02-18 02:02:54

标签: 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;
    }
}

5 个答案:

答案 0 :(得分:4)

我将以下列方式做 创建一个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();
        }
    };
}

然后使用下面的代码启动应该在UI Thread

中运行的代码
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
        }
    }));
下面的

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

    @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));

}

答案 1 :(得分:2)

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;
    }


}

You can start your fragment from UI thread as mentioned above.

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您可以使用FragmentTestRule

您必须使用:

而不是常规ActivityTestRule
@Rule
public FragmentTestRule<?, FragmentWithoutActivityDependency> fragmentTestRule =
    FragmentTestRule.create(FragmentWithoutActivityDependency.class);

您可以找到more details in this blog post

答案 4 :(得分:0)

如果可以使用androidx库的alpha版本,则可以使用if !option! geq 1 if !option! leq 10 goto :NEXT echo Invalid option !option! goto :EOF :NEXT echo !option! is greater than or qual to 1, and is less than or equal to 10 库。在撰写本文时,最新版本是androidx.fragment:fragment-testing

在测试方法中启动片段很简单:

1.1.0-alpha07

您可以找到有关此库here的更多信息。