snapshotino

一个轻量级、低门槛的 iOS 快照测试库

为什么使用快照测试?

这个库的功能

就这些!

这个库肯定不能做什么

这个库可能不能做什么

作者注

如果你正在寻找一个将在你的生产应用程序中使用的库,你可能应该考虑更强大的工具,例如 pointfreeco/swift-snapshot-testinguber/ios-snapshot-test-case。这个库更像是一个个人项目,而不是一个成品。我只是想为我为了乐趣而构建的小型应用程序和组件进行快照测试,而无需为我有限的用例导入一个成熟的第三方库。

作为一个不太有趣的事实,这也是为什么我试图在项目名称中暗示这个项目的规模很小,通过使用 -ino 后缀,这似乎是意大利语中常见的指小后缀。

如何使用

对于录制快照

只需通过将 record 设置为 true 来调用 assertSnapshot。测试将失败,但快照将被记录在测试文件旁边的 __snapshots__ 文件夹下。

func testUIViewController() throws {
    try assertSnapshot(of: SampleViewController(), record: true)
}

对于断言快照

调用 assertSnapshot 而不设置 record 属性(默认情况下为 false)。

func testUIViewController() throws {
    try assertSnapshot(of: SampleViewController())
}

func testUIView() throws {
    if let view = Bundle.main.loadNibNamed("SampleView", owner: self, options: nil)?.first as? SampleView {
        try assertSnapshot(of: view)
    }
}

func testView() throws {
    let view = ContentView(viewModel: .init(imageName: "circle", text: "test"))
    try assertSnapshot(of: view.asSnapshottableView)
}

请注意,SwiftUI.View 不能直接使用,因为它不是一个具体的类型。我们改用 asSnapshottableView 属性,它将返回一个可以传递给 assertSnapshot 的包装器类型。

示例项目

https://github.com/kublaios/KleinSnapshotTesting

许可证

本项目根据 MIT 许可证发布。有关详细信息,请参阅 LICENSE