Skip to content

Add support for Compose Preview Screenshot Testing #586

@dfabulich

Description

@dfabulich

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.

Example screenshot

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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesttestingIssues related to Skip's testing capabilities

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions