Any recommendations on the best way to use Fragments with Paparazzi? #416
-
Paparazzi tests work off of Views, but what if an app primarily uses Fragments for its screens? Are there patterns for instantiating a Fragment in a test and extracting its root layout view for use with Paparazzi? I know you guys at Square typically shy away from Fragment usage and use Views directly, but I'm curious if someone in the community has tried to incorporate Paparazzi with Fragments, or any of the Fragment testing utilities, with any success. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This is a frequent mischaracterization of what we do. We don't use fragments simply because they're poorly designed abstractions that do more harm than good. We have strong layering and encapsulation of our data layer, our presenter layer, and our rendering layer (where Views live). The rendering layer is regular views (not necessarily "custom") or Compose UI. The presentation layer speaks to the rendering layer through well-defined interfaces which means it's easy to drive the rendering layer with fake data. Some people don't have the flexibility to abandon fragments. That's unfortunate, because they tend to muddy up clean layering and make otherwise simple things hard. Think about testing just a This is how our rendering layer works. You can efffectively Fragments, if you have to use them, are the place where you ideally put a bunch of glue code. It's where you glue together well-encapsulated components like presenters and views which can each exists and be tested on their own so you never actually have to test the fragments (except maybe implicitly in UI-based instrumentation tests). Your presenters can be tested on the JVM in unit tests and your views can be tested either in unit tests inside instrumentation or on the JVM with something like paparazzi. I know it's not fun hearing that you have to do a bunch of work to make paparazzi or other forms of testing work when you're using fragments. But that's the reality when Google continues to push an abstraction that violates clean layering and actively inhibits good testing practices.
So, concretely, this is something that I would migrate to view binding which provides a single object that holds all references for you, or migrate your references to a plain old Java/Kotlin object. That object can have functions like |
Beta Was this translation helpful? Give feedback.
-
Thanks for the detailed response, Jake. I appreciate the extra context!
This is pretty close to what I was brainstorming in my head, and sounds reasonable given the fact that we'd have to incrementally adopt Paparazzi layout-by-layout anyway 👍 |
Beta Was this translation helpful? Give feedback.
This is a frequent mischaracterization of what we do.
We don't use fragments simply because they're poorly designed abstractions that do more harm than good. We have strong layering and encapsulation of our data layer, our presenter layer, and our rendering layer (where Views live). The rendering layer is regular views (not necessarily "custom") or Compose UI. The presentation layer speaks to the rendering layer through well-defined interfaces which means it's easy to drive the rendering layer with fake data.
Some people don't have the flexibility to abandon fragments. That's unfortunate, because the…