This example shows how to migrate from swift-snapshot-testing to preview providers. The example app contains a unit test target UnitTestMigrationTests with one snapshot test. It has been migrated by copying/pasting the file into the main app target as ExampleSnapshotTest_Migrated.swift. The test is modified by removing the XCTest/SnapshotTesting importants, changing the base class to SnapshotTest and adding the conformance to PreviewProvider.
- Add the file SnapshotTest.swift to your app, this does the heavy lifting of converting snapshot test function calls into previews.
- Copy your test file from the unit test target to your application.
- Remove imports of
XCTestandSnapshotTesting. - Change the base class from
XCTestCasetoSnapshotTestand add a conformce toPreviewProvider.
Here’s a complete example of a migrated test:
import SwiftUI
// Add the conformance to `PreviewProvider` when extending `SnapshotTest`
// The `SnapshotTest` base class automatically handles turning calls to
// assertSnapshot into previews.
class ExampleSnapshotTest: SnapshotTest, PreviewProvider {
func testContentViewSnapshot() {
assertSnapshot(of: ContentView(), as: .image)
}
}