Build License Version

静物 (Stilleben)

用 Swift 编写的现代快照测试框架。

主要用于测试 UI 组件的快照,但也支持模型和其他类型的快照。

它自带一些预定义的匹配器

快照可以配置使用以下策略

示例

SwiftUI 视图的快照测试示例

import XCTest
import Stilleben

final class SomeViewTests: XCTestCase {
    private let matcher = UIMatcher()
        .assertSimulator(modelIdentifier: "iPhone14,7")
        .sizing(.dynamicHeight)
        .dynamicTypeSizes(.large)

    func testSomeView() async throws {
        await matcher
            .colorSchemes(.light)
            .match {
                NavigationView {
                    SomeView()
                }
            }
    }
}
生成的快照

它还支持快照测试 UIKit 视图控制器或视图

import XCTest
import Stilleben

final class SomeViewControllerTests: XCTestCase {
    private let matcher = UIMatcher()
        .assertSimulator(modelIdentifier: "iPhone14,7")
        .sizing(.screen)
    
    func testSomeViewController() async throws {
        await matcher
            .match { @MainActor () -> UIViewController in
                SomeViewController()
            }
    }
    
    func testSomeView() async throws {
        await matcher
            .sizing(.intrinsic)
            .match { @MainActor () -> UIView in
                SomeView()
            }
    }
}

支持快照模型的反射

import XCTest
import Stilleben

final class ReflectionTests: XCTestCase {
    let matcher = ReflectionMatcher()

    func testSimpleModel() async {
        struct Model {
            let text = "Hello World!"
        }

        await matcher.match {
            Model()
        }
    }
}

这将生成一个文本文件快照

  ▿ StillebenTests.ReflectionTests.?.?.Model
    - text: "Hello World!"