Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Writing test cases for Fragments

In my previous projects I've done most of the work through Activities and used ActivityInstrumentationTestCase2 as per the document:
http://developer.android.com/tools/testing/activity_testing.html
I have an idea how to work with Activity Test cases; but when it comes to Fragment ,I don't have much idea nor found much documents related to that. So how to write test cases when I have several fragments with one or two actvities? Any example code or sample would be more helpful.

like image 986
AnswerDroid Avatar asked Jun 18 '15 07:06

AnswerDroid


People also ask

Are fragments still used in Android?

Using the support library, fragments are supported back to all relevant Android versions. Fragments encapsulate views and logic so that it is easier to reuse within activities.

How do you write test cases for Android Apps?

Test cases should be written in such a way that they allow a person to test only one feature at a time. One should not overlap or complicate test cases. Cover all the positive and negative probabilities of the test outcomes. Write in simple language with exact and accurate names of forms, fields, etc.


2 Answers

Here's a rough guide using ActivityInstrumentationTestCase2:

Step 1. Create a blank Activity to hold your fragment(s)

  private static class FragmentUtilActivity extends Activity {     @Override     protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       LinearLayout view = new LinearLayout(this);       view.setId(1);        setContentView(view);     }   } 

Step 2: Inside your test, instantiate your fragment and add it to the blank activity

public class MyFragmentTest extends ActivityInstrumentationTestCase2<FragmentUtilActivity> {      private MyFragment fragment;      @Before     public void setup() {         fragment = new MyFragment();         getActivity().getFragmentManager().beginTransaction().add(1, fragment, null).commit();     } } 

Step 3 Test your instantiated fragment

@Test public void aTest() {     fragment.getView().findViewById(...); } 

If you're using robolectric, this is pretty straightforward using the FragmentUtilTest class:

@Test public void aTest() {     // instantiate your fragment     MyFragment fragment = new MyFragment();      // Add it to a blank activity     FragmentTestUtil.startVisibleFragment(fragment);      // ... call getView().findViewById() on your fragment } 
like image 87
Nachi Avatar answered Oct 02 '22 15:10

Nachi


Here is my working solution:

  1. Create an instrumentation unit test class for this in androidTest directory, i.e.:

    public class FragmentTest extends  ActivityInstrumentationTestCase2<MainActivity> {      private MainActivity testingActivity;      private TestFragment testFragment;      //...  } 
  2. call this constructor inside this new class:

    public FragmentTest() {      super(MainActivity.class); } 
  3. override the setUp() method (be sure to have R.id.fragmentContainer in your Activity class) where you will call at the end waitForIdleSync():

    @Override protected void setUp() throws Exception {     super.setUp();      // Starts the activity under test using     // the default Intent with:     // action = {@link Intent#ACTION_MAIN}     // flags = {@link Intent#FLAG_ACTIVITY_NEW_TASK}     // All other fields are null or empty.     testingActivity = getActivity();      testFragment = new TestFragment();     testingActivity.getFragmentManager().beginTransaction().add(R.id.fragmentContainer,testFragment,null).commit();     /**      * Synchronously wait for the application to be idle.  Can not be called      * from the main application thread -- use {@link #start} to execute      * instrumentation in its own thread.      *      * Without waitForIdleSync(); our test would have nulls in fragment references.      */     getInstrumentation().waitForIdleSync(); } 
  4. Write a test method, for example somethng like:

    public void testGameFragmentsTextViews() {     String empty = "";    TextView textView = (TextView)testFragment.getView().findViewById(R.id.myTextView);    assertTrue("Empty stuff",(textView.getText().equals(empty)));    } 
  5. Run the test.

like image 28
Michał Dobi Dobrzański Avatar answered Oct 02 '22 16:10

Michał Dobi Dobrzański