https://github.com/dfabulich/skipapp-compose-snapshot-sample
This is a Skip Lite dual-platform app project, written mostly in Swift that transpiles to Kotlin, demonstrating how to setup and run Compose Preview Screenshot Testing.
What's Compose Preview Screenshot Testing?
In Compose Preview Screenshot Testing, you write @Preview components (like SwiftUI #Preview, but in Jetpack Compose) and decorate them with @PreviewTest. An update build plugin captures screenshots of all of your @PreviewTest components and writes them to disk; you commit them with your code.
Later, you run your tests again in validate mode. The tests fail if any of your components render differently. You then review the updated screenshots in a generate HTML report; if you approve of all of the changes, rerun update, which wil regenerate all of the snapshots.
Writing a test
@PreviewTest
@Preview(showBackground = true)
@Composable
fun WelcomeViewPreview() {
ProcessInfo.launch(LocalContext.current)
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
WelcomeView(welcomeName = Binding.constant("Skipper"))
.Compose(context = ComposeContext())
}
}
Running tests
Generate screenshot test results:
gradle -p Android -Pandroid.experimental.enableScreenshotTest=true updateDebugScreenshotTest
Validate that screenshot test results are unchanged:
gradle -p Android -Pandroid.experimental.enableScreenshotTest=true valdateDebugScreenshotTest
(We have to use -P because setting this in Android/app/gradle.properties change doesn't apply to all of the projects that Skip generates; see below.)
https://github.com/dfabulich/skipapp-compose-snapshot-sample