29

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 problem.

How can I test a Fragment which is not added to an Activity in onCreate. All examples I have seen with Fragments involve the Fragment being added in onCreate. So how can I tell Espresso to go to a specific Fragment and start from there?

Thanks

thaussma
  • 9,756
  • 5
  • 44
  • 46
Ersen Osman
  • 7,067
  • 8
  • 47
  • 80

4 Answers4

11

If you are using the Navigation Architecture component, you can test each fragment instantly by Deep linking to the target fragment (with appropriate arguments) at the beginning of the test.

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

protected fun launchFragment(destinationId: Int,
                             argBundle: Bundle? = null) {
    val launchFragmentIntent = buildLaunchFragmentIntent(destinationId, argBundle)
    activityRule.launchActivity(launchFragmentIntent)
}

private fun buildLaunchFragmentIntent(destinationId: Int, argBundle: Bundle?): Intent =
        NavDeepLinkBuilder(InstrumentationRegistry.getInstrumentation().targetContext)
                .setGraph(R.navigation.navigation)
                .setComponentName(MainActivity::class.java)
                .setDestination(destinationId)
                .setArguments(argBundle)
                .createTaskStackBuilder().intents[0]

destinationId being the fragment destination id in the navigation graph. Here is an example of a call that would be done once you are ready to launch the fragment:

launchFragment(R.id.target_fragment, targetBundle())

private fun targetBundle(): Bundle? {
    val bundle = Bundle()
    bundle.putString(ARGUMENT_ID, "Argument needed by fragment")
    return bundle
}

Also answered in more detail here: https://stackoverflow.com/a/55203154/2125351

Sean Blahovici
  • 5,350
  • 4
  • 28
  • 38
5

So, according to patterns and recommended practices by several companies. You need to write targeted and hermetic test for each view, whether it is activity, fragment, dialog fragment or custom views.

First you need to import the following libs into your project through gradle if you are using gradle in the following way

debugImplementation 'androidx.fragment:fragment-testing:1.2.0-rc03'
debugImplementation 'androidx.test:core:1.3.0-alpha03'

For Kotlin, debugImplementation 'androidx.test:core-ktx:1.3.0-alpha03'

In order to test fragment independently from activity, you can start/launch it in the following way:

@Test
    fun sampleTesting(){
        launchFragmentInContainer<YourFragment>()
        onView(withId(R.id.sample_view_id)).perform(click())
    }

This way, you can independently test your fragment from your activity and it is also one of recommended way to achieve hermetic and targeted ui test. For full details, you can read the fragment testing documentation from android docs

https://developer.android.com/training/basics/fragments/testing

And I found this repository with lots of test example useful even though it is extremely limited to complexity of test cases with super simple tests. Though it can be a guide to start.

https://github.com/android/testing-samples

Jujare Vinayak
  • 121
  • 1
  • 8
Farruh Habibullaev
  • 2,342
  • 1
  • 26
  • 33
-2

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)
}
StefanTo
  • 971
  • 1
  • 10
  • 28
  • I didn't look into it exactly, maybe it takes some time for the FragmentManager to commit the new state, maybe it was just an animation that you could disable easily on test devices... however I think it'd be overengineered to create an idling resource for FragmentManager in a test code snippet... Or how would your solution look like? – StefanTo Sep 20 '18 at 16:41
-12

Espresso can test Fragments only if they are displayed. And that requires them to be displayed by an Activity.

With your current setup you'll have to use Espresso to click() your way (like a user would) to the Fragment you actually want to test.

In one of my projects I have a ViewPager that displays Fragments. For those Fragments I use a custom FragmentTestRule to test them in isolation. I can start each Fragment directly and use Espresso to test it. See this answer.

You could also:

  • Do not use Fragments. Activities are easier to test. You can test each Activity on its own. In most cases Fragments offer no advantage over Activities. Fragments just make the implementation and testing more difficult.
  • Enable your FragmentActivity to directly show a certain Fragment when it is created. E.g. by supplying a special intent extra to your FragmentActivity. But this would add testing code to your app, which is generally not a good solution.
Community
  • 1
  • 1
thaussma
  • 9,756
  • 5
  • 44
  • 46
  • [FragmentTestRule](https://github.com/21Buttons/FragmentTestRule) it's an implematation of the linked answer. – Brais Gabin Aug 16 '17 at 09:26
  • 15
    Fragments load faster than activities (they can even be pre-loaded into memory), they allow for modularization and reuse of components, and aren't required to be displayed full screen (they can be placed in a layout just like any other view). Fragments are very popular because they improve upon an Activity a lot. Saying Fragments offer no advantage is incredibly inaccurate, and the benefit of using them far outweighs the testing difficulty. – OldSchool4664 Sep 19 '17 at 21:34
  • I am Sorry, but **"Not using Fragments"**, is not a solution. Fragments are damn reusable & faster as compared to Activities. – Pawan Jun 13 '18 at 14:10
  • 1
    Yes they are useful. And this is just a suggestion. If you don't know how to test them, maybe try a testable approach... I know how to test them and I use them too. – thaussma Jun 13 '18 at 15:35
  • I suggest you remove (if you like) the fragment suggestion, or to specify that is your favourite preference, so that users can upvote you as `I just did, and in the future juniors reading your reply will understand that the first part of your response was perfectly valid – Drocchio Dec 19 '18 at 15:33