android - 使用 fragment 对 Android 应用程序进行 JUnit 测试

标签 android unit-testing android-fragments junit

我的 Android 应用程序是基于单一 Activity 、基于多个 fragment 的模型构建的。 我需要对应用程序进行单元测试。我可以为包含使用 ActivityInstrumentationTestCase2 JUnit 的所有 Activity 的应用程序编写单元测试用例,但不能为包含 fragment 的应用程序编写单元测试用例。 请建议为 fragment 编写 JUnit 测试用例的方法。

谢谢

最佳答案

参见 Android: Testing fragments

Copied for your reading pleasure with edits made for getFragmentManager() vs getSupportFragmentManager() and android:exported="false":

如果您想单独测试一个 fragment ,您需要创建一个测试 FragmentActivity,以便您的测试可以使用它。测试 Activity 看起来像这样。请记住在您的应用程序的 list 中声明它:

 public class TestFragmentActivity extends FragmentActivity {
   @Override
   protected void onCreate(Bundle arg0) {
     super.onCreate(arg0);
     setContentView(R.layout.activity_fortests);
   }
 }

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
   <LinearLayout
        android:id="@+id/activity_test_fragment_linearlayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        />
 </RelativeLayout>

安卓 list :

 ...
 <activity
  android:name="your.package.name.TestFragmentActivity"
  android:exported="false" />
 ...

然后在你的测试项目中,你可以有一个这样的类来启动 fragment :

  public class FrameworkObjectsGeneratorFragmentTest 
      extends ActivityInstrumentationTestCase2<TestFragmentActivity> {
    private TestFragmentActivity mActivity;

    public FrameworkObjectsGeneratorFragmentTest() {
      super(TestFragmentActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
      super.setUp();
      mActivity = getActivity();
    }

    private Fragment startFragment(Fragment fragment) {
      FragmentTransaction transaction = mActivity.getFragmentManager().beginTransaction();
      transaction.add(R.id.activity_test_fragment_linearlayout, fragment, "tag");
      transaction.commit();
      getInstrumentation().waitForIdleSync();
      Fragment frag = mActivity.getFragmentManager().findFragmentByTag("tag");
      return frag;
    }

   public void testFragment() {
      FrameworkObjectsGeneratorFragment fragment = new FrameworkObjectsGeneratorFragment() {
         //Override methods and add assertations here.
      };

      Fragment frag = startFragment(fragment);
    }
  }

startFragment() 方法将您指定的 fragment 添加到 TestActivity 中的 ViewGroup。

与 Activity 相比,测试 fragment 的好处在于您可以扩展 fragment 以覆盖 protected 字段和方法,您可以在其中添加断言。

注意:如果您正在使用支持库,请调用 getSupportFragmentManager()。

关于android - 使用 fragment 对 Android 应用程序进行 JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21156463/

相关文章:

android - HttpConnection 中的 setAllowUserInteraction() 是什么

连接到 mysql 服务器的 Android 应用程序返回用 [] 、 {} 和双引号括起来的值

java - 如何设置ImageResource列表<Item>

asp.net - 单元测试未通过,因为没有看到 api Controller 的任何返回

android - 选择图像时 onActivityResult() 返回 Activity 的第一个 fragment

android - 声音池 : sample 1 not READY

java - @ReplaceWithMock 与 @Qualifier

python - 将 python mockito 与 python unittest 集成

Android TabLayout PagerAdapter 不显示第一个选项卡 fragment

android - 通过对话框更新 fragment ListView 中显示的数据库条目