JUnit的测试与碎片Android应用碎片、测试、JUnit、Android

2023-09-06 03:06:10 作者:没有你的夜温度总不够

我的Andr​​oid应用程序是建立在单一的活动,多个片段基础的模式。 我需要做单元测试的应用程序。我可以写单元测试用例的应用程序,它包含了使用ActivityInstrumentationTestCase2 JUnit的所有活动,而不是应用程序,其中包含的片段。 请建议写的JUnit测试案例的片段的方式。

My Android App was built on Single Activity, multiple fragments based model. I need to do unit testing for the app. I could write unit testcases for app which contains all activities using ActivityInstrumentationTestCase2 JUnit but not for app which contains fragments. Please suggest the way to write JUnit testcases for fragments.

感谢您

推荐答案

请参阅 安卓测试片段

See Android: Testing fragments

复制您的阅读乐趣与getFragmentManager()与getSupportFragmentManager()和Android进行了编辑:出口=假:

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

如果你想测试孤立的片段,你需要创建一个测试FragmentActivity让您的测试可以使用。该测试活动将是这个样子。请记住,在你的应用程序的清单声明它:

If you want to test a fragment in isolation, you need to create a Test FragmentActivity so your test can use that. The test activity will look something like this. Remember to declare it in your application’s manifest:

 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>

AndroidManifest:

AndroidManifest:

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

然后在您的测试项目,你可以有一类这样开始的片段:

Then in your test project, you can have a class like this to start the 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()方法添加一个片段指定到的ViewGroup在TestActivity。

The startFragment() method adds a fragment you specify to the ViewGroup in the TestActivity.

有关测试片段,而不是活动,的好处是,你可以扩展片段覆盖保护的领域和方法,在其中您可以添加断言。

The good thing about testing fragments, as opposed to Activities, is that you can extends the Fragment to override protected fields and methods within which you can add assertions.

注意:调用getSupportFragmentManager()如果您使用的是支持库

 
精彩推荐