它是 SnapshotTesting 的一个扩展,允许您创建图像,将多个快照策略的输出组合起来,前提是它们都输出到 UIImage
。
本质上,这允许您拥有一个代表应用程序中单个视图的图像,并在多个不同的配置中显示。 例如,当您想在多个设备上或在明暗模式下可视化同一个 UIViewController 时,这可能很有用。
图像也可以有标题,允许您轻松识别图像中的每个配置。
一旦 安装 完成,无需额外配置。 您可以导入 SnapshotTestingStitch
模块,按照他们的使用指南调用 SnapshotTesting
,并简单地提供我们的 stitch
策略,如下所示。
import SnapshotTesting
import SnapshotTestingStitch
import XCTest
class MyViewControllerTests: XCTestCase {
func testMyViewController() {
let vc = MyViewController()
assertSnapshot(matching: vc, as: .stitch(strategies: [
.image(on: .iPhone8),
.image(on: .iPhone8Plus),
]))
}
}
默认情况下,如果您只提供一个策略数组,那么这些策略将没有标题。 如果您提供一个包含字符串和策略的元组,则该字符串将作为标题放置在快照中的图像上方。
assertSnapshot(matching: vc, as: .stitch(strategies: [
("iPhone 8", .image(on: .iPhone8)),
("iPhone 8 Plus", .image(on: .iPhone8Plus)),
]))
stitch
策略的可选参数是 "style"。 这个值允许您自定义包生成的渲染快照的某些部分。
这包括图像周围的间距、使用的颜色以及可以围绕每个图像的可选边框。 边框对于清楚地识别每个图像的边界非常有用 - 尤其是当图像背景与快照背景相同时。
已经为您提供了合理的默认值。
assertSnapshot(matching: vc, as: .stitch(strategies: [
("iPhone 8", .image(on: .iPhone8)),
("iPhone 8 Plus", .image(on: .iPhone8Plus)),
], style: .init(
fontSize: 20,
titleColor: .white,
borderColor: .red,
borderWidth: 5,
itemSpacing: 32,
framePadding: 32,
titleSpacing: 32,
backgroundColor: .black
)))
⚠️ 警告:默认情况下,Xcode 会尝试将 SnapshotTestingStitch 包添加到您项目的主应用程序/框架目标中。 请确保按照下面的最后一步中的说明,将 SnapshotTestingStitch 添加到测试目标中。
https://github.com/Sherlouk/swift-snapshot-testing-stitch
如果您想在任何其他使用 Swift Package Manager 的项目中使用 SnapshotTestingStitch,请在 Package.swift
中将该包添加为依赖项
dependencies: [
.package(name: "SnapshotTestingStitch", url: "https://github.com/Sherlouk/swift-snapshot-testing-stitch.git", from: "1.0.0"),
]
接下来,将 SnapshotTestingStitch
添加为测试目标的依赖项
targets: [
.target(
name: "MyApp"
),
.testTarget(
name: "MyAppTests",
dependencies: [
.target(name: "MyApp"),
.product(name: "SnapshotTestingStitch", package: "SnapshotTestingStitch"),
]
),
]
我们目前不支持通过 CocoaPods 或 Carthage 进行分发。
该库是在 MIT 许可证下发布的。 有关详细信息,请参见 LICENSE。