4

ScreenShot Testing Composable with Paparazzi Library 📸 📸

 1 year ago
source link: https://medium.com/@10kanake/screenshot-testing-composable-with-paparazzi-library-e766006d0705
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

ScreenShot Testing Composable with Paparazzi Library 📸 📸

Hello guys, in today’s article we are going to see how we can test our composable in android development using Paparazzi library by CashApp.

Pre-requisite.

Paparazzi does not run on monolith applications and hence we shall have to modularise our app.

Our first step with be, creating a module in android studio.In this module we are going to create the composable we shall test.Before this remember to add the paparazzi dependency root module inclusive of the paparazzi plugin. NB:Paparazzi plugin is added to the module with composables to be tested.

classpath "app.cash.paparazzi:paparazzi-gradle-plugin:VERSION"
1*bC1f4GeKRwaMlitkO-Q5bQ.png

Onto the next step which is creating our composable.Our view will be a simple card with two text views and one Icon.

1*Ow5GsFfcXPfG_rMpM0TNHA.png

Finally to the interesting bit.Testing the composable.Since paparazzi does not require emulator we are going to write our code in the test folder of the module with our composable still.

This the code test for our test.

1*sb4rbs7JdYFMINMYoUMxgg.png

But this not the end of our testing.Paparazzi library provides commands to run in order to take snapshot of our composable and also to verify if the images from our composable and rendered by the library match.

to generate our snapshot,run the following command on your terminal 
./gradlew recordPaparazziDebug

To verify that your changes haven't broken any Paparazzi tests, you can run the following command:

./gradlew verifyPaparazziDebug

After generating our snapshots, you will realize that android studio will create a snapshots folder with an image of the composable.This is the image that shall be used as the prove while verifying.

Take Away 🥡 🥡

In the jvm rule, we can specify a specific device you want to use, a default theme etc.In such cases the testparameter injector library by Google comes in handy. Have a read for their documentation here TestParameterInjector.

Example from the paparazzi docs

1*mFcKRTCuQCRbnZ2uNhHs3Q.png

It’s also possible to create a helper function where you can render your components eg in a box with certain dimensions and replace the .snapshot{} paparazzi function with it.

If you any question get me on Twitter @no0k3h

Other Great Articles


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK