一个轻量级、低门槛的 iOS 快照测试库
SwiftUI.View、UIView 和 UIViewController 的快照就这些!
如果你正在寻找一个将在你的生产应用程序中使用的库,你可能应该考虑更强大的工具,例如 pointfreeco/swift-snapshot-testing 或 uber/ios-snapshot-test-case。这个库更像是一个个人项目,而不是一个成品。我只是想为我为了乐趣而构建的小型应用程序和组件进行快照测试,而无需为我有限的用例导入一个成熟的第三方库。
作为一个不太有趣的事实,这也是为什么我试图在项目名称中暗示这个项目的规模很小,通过使用 -ino 后缀,这似乎是意大利语中常见的指小后缀。
XCTestCase 实现中导入 snapshotinoassertSnapshot 录制和比较快照,示例如下只需通过将 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。