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
Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.
To pass data from one fragment to another Bundle will help. LifeShapeDetailsFragment fragment = new LifeShapeDetailsFragment(); // object of next fragment Bundle bundle = new Bundle(); bundle. putInt("position", id); fragment. setArguments(bundle);
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With