Android Espresso: How do I test a specific Fragment when following one activity to several fragment architecture

后端 未结 4 658
刺人心
刺人心 2021-02-12 10:52

My app consists of one Activity for many Fragments.

I wish to use Espresso to test the UI of the Fragments. However I ran into a p

4条回答
  •  盖世英雄少女心
    2021-02-12 11:19

    Just show the Fragment using the Activity's SupportFragmentManager.

    For example (Kotlin) with ActivityTestRule:

    @Rule
    @JvmField
    var activityRule = ActivityTestRule(MainActivity::class.java)
    

    Just do this before your tests:

    @Before
    fun setup() {
        activityRule.activity.supportFragmentManager.beginTransaction().replace(R.id.main_activity_container_for_your_fragments, FragmentToShow(), "fragment-tag").commitAllowingStateLoss()
        Thread.sleep(500)
    }
    

提交回复
热议问题