简体   繁体   中英

Test Fragment with espresso lib

My app has only one activity and based on many fragments. How I can test this fragment in a right way? Give me an example, please. Cause I try this test class:

    @LargeTest
public class ActivityTest extends ActivityInstrumentationTestCase2<ActivityEx> {
public ActivityTest() {
    super(ActivityEx.class);
}

public void setUp() throws Exception {
    super.setUp();
    getActivity();
}

public void testTest() {
  //simple example
    assertEquals(true, true);
}

}

And in result I've failed due to ClassCastException.

Rather do it like this:

public class ActivityTest extends android.test.ActivityInstrumentationTestCase2
{
    public ActivityTest()
    {
        super(ActivityEx.class);
    }

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

    public void testTest() {
        //simple example
        assertEquals(true, true);
    }
}

If you interested I also posted a tutorial on testing fragments http://www.stevenmarkford.com/testing-fragments-with-android-espresso-basic-example/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM