Skip to content

Paparazzi

akshay-at-yml edited this page Aug 22, 2022 · 5 revisions

Its an Android library which can render application UI without the need of android emulator or a physical device. It will also allows to capture snapshots of the rendered UI, and perform screenshot testing.

Paparazzi tests are not instrumentation tests as emulators or physical devices are not required. Paparazzi APIs are only accessible in the unit test folder.

Writing Tests

In the Unit test class, create paparazzi instance. paparazzi.snapshot{} will allow to call any composable functions and takes a snapshot of it.

@get:Rule
val paparazzi = Paparazzi(
    maxPercentDifference = PERCENT_DIFFERENCE,
		// device configurations can be specifies 
    deviceConfig = PIXEL_4_XL,
)
....

@Test
fun testMethod() {
	paparazzi.snapshot {
			//Call any composable methods to be tested
			// Can write various combinations of theme, layout directions using CompositionLocalProvider
	}
}

Multiple Device Configurations

Paparazzi instance will allow to create Device configurations on which the tests can be run. It has a set of predefined device configurations like PIXEL_4_XL, NEXUS_5_LAND etc.

If you want to change the device later while running tests, use below API.

paparazzi.unsafeUpdateConfig(// Pass device config here)

Tolerance

You can set the tolerance by setting maxPercentDifference when creating the Paparazzi instance. Values will range from 0.1 to 1.0

Example : If maxPercentDifference = 0.1 ⇒ The tests will be passed until the change exceed 0.1

Running Tests

Record

To record the screenshots ./gradlew <module-name>:recordPaparazziDebug

Verify

To verify the screenshots against golden values, ./gradlew <module-name>:verifyPaparazziDebug

💡 Recommended to clean the module before recording or verifying ./gradlew <module-name>:clean

Initial Setup

Add the following plugin dependency in the root project.

dependencies {
    classpath 'app.cash.paparazzi:paparazzi-gradle-plugin:1.0.0'
}

Apply the plugin in the module where Paparazzi tests should be written.

apply plugin: 'app.cash.paparazzi'