Improving snapshot tests with Paparazzi

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • SaaSHub - Software Alternatives and Reviews
  • Shot

    Screenshot testing library for Android

  • I previously wrote about snapshot testing Compose with Shot here: https://medium.com/proandroiddev/oh-snap-966bd786b7a4

  • paparazzi

    Render your Android screens without a physical device or emulator

  • /** * Finds all files in the components module which have Compose previews * and generates Paparazzi screenshot tests for them. * * The generated tests can then be used to record screenshots with * ./gradlew components:recordPaparazziInternalDebug * * To verify that the current implementation matches the recorded screenshots * ./gradlew components:verifyPaparazziInternalDebug */ fun main() { val path = System.getProperty("user.dir") ?: error("Can't get user dir") // Paparazzi does not currently work in the app module: https://github.com/cashapp/paparazzi/issues/107 // For now this is hardcoded to only check files in the components module. // If we pull our compose files out of the app module to a separate module this code has to be updated. File(path).walk().filter { it.path.contains("/components/src/main/java") && it.extension == "kt" }.forEach { if (it.readText().contains("@Preview")) { processFileWithPreviews(it) } } } /** * Reads the given file, finds the names of all the functions annotated with @Preview * and uses them to generate a Paparazzi test file with one test for each preview. */ private fun processFileWithPreviews(file: File) { val lines = file.readLines() val previewNames = mutableListOf() var saveNextFunctionName = false var packageName = "" lines.forEachIndexed { i, line -> if (i == 0) { packageName = line.split(" ").last() } if (line.contains("@Preview")) { saveNextFunctionName = true } if (saveNextFunctionName && line.startsWith("fun ")) { previewNames += line.split(" ")[1].removeSuffix("()") saveNextFunctionName = false } } val pathString = file.path.replace("src/main", "src/test").split("java").first() + "java" val testFilePath = pathString.toPath() generatePaparazziTest(packageName, file.nameWithoutExtension + "PaparazziTest", testFilePath, previewNames) } fun generatePaparazziTest(packageName: String, fileName: String, path: Path, previewNames: List) { val classBuilder = TypeSpec.classBuilder(fileName) .superclass(PaparazziTest::class) .addAnnotation( AnnotationSpec.builder(Suppress::class) // KotlinPoet does not let us remove redundant public modifiers or Unit return types for the functions, // but we don't mind for generated code as long as the tests work .addMember("\"RedundantVisibilityModifier\", \"RedundantUnitReturnType\"") .build() ) previewNames.forEach { classBuilder.addFunction( FunSpec.builder(it.removeSuffix("Preview").usLocaleDecapitalize()) .addStatement("paparazziRule.snapshot { $it() }") .addAnnotation(Test::class) .build() ) } val testFile = FileSpec.builder(packageName, fileName) .addType(classBuilder.build()) .addFileComment("AUTO-GENERATED FILE by generate_paparazzi_tests.kt\nDO NOT MODIFY") .build() val nioPath = path.toNioPath() testFile.writeTo(nioPath) }

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
  • kotlinpoet

    A Kotlin API for generating .kt source files.

  • To do this we created a Kotlin program with a main function and used another library, also from Square, KotlinPoet (https://github.com/square/kotlinpoet) to generate the code.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts