示例工程 57-ios-quality-gates
57-ios-quality-gates/Tests/Ch57KitTests/QualityTests.swift
import SwiftUI
import Testing
@testable import Ch57Kit
@Suite("设备矩阵")
struct DeviceMatrixTests {
@Test func hasThreeRequiredDevices() {
#expect(DeviceMatrix.required.count == 3)
}
@Test func destinationsUseIOS26() {
#expect(DeviceMatrix.destinations.allSatisfy { $0.contains("OS=26.0") })
}
@Test func unavailableDevicesAreSkipped() {
let devices = [
SimulatorDevice(name: "iPhone 17", runtime: "iOS 26.0", available: true),
SimulatorDevice(name: "iPad", runtime: "iOS 26.0", available: false),
]
#expect(devices.filter(\.available).count == 1)
}
}
@Suite("门禁")
struct GateTests {
@Test func summaryCountsPassAndFail() {
let gates = [
QualityGate(name: "a", passed: true, detail: ""),
QualityGate(name: "b", passed: false, detail: ""),
]
let summary = GateRunner.summarize(gates)
#expect(summary.passed == 1)
#expect(summary.failed == 1)
#expect(!GateRunner.allPassed(gates))
}
@Test func emptyGatesAllPassVacuously() {
#expect(GateRunner.allPassed([]))
}
}
@Suite("测试命令")
struct TestCommandTests {
@Test func buildsDestinationArguments() {
let command = TestCommand(
project: "App.xcodeproj",
scheme: "App",
destinations: ["platform=iOS Simulator,name=iPhone 17,OS=26.0"],
resultBundle: "build/App.xcresult"
)
#expect(command.arguments.contains("-project"))
#expect(command.arguments.contains("-resultBundlePath"))
#expect(command.arguments.last == "test")
#expect(command.isValid)
}
@Test func emptyDestinationsAreInvalid() {
let command = TestCommand(project: "a", scheme: "b", destinations: [], resultBundle: "c")
#expect(!command.isValid)
}
}
@Suite("体积")
struct SizeTests {
@Test func withinBudget() {
#expect(SizeReport(downloadMB: 50, installMB: 150).withinBudget)
#expect(!SizeReport(downloadMB: 50, installMB: 250).withinBudget)
}
@Test func ratioUsesInstallSize() {
let report = SizeReport(downloadMB: 10, installMB: 100, budgetMB: 200)
#expect(report.ratio == 0.5)
}
}
@Suite("视图")
struct QualityViewTests {
@Test @MainActor func boardRendersAtFixedSize() throws {
let image = try #require(ImageRenderer(content: QualityBoard()).cgImage)
#expect(image.width == 340)
#expect(image.height > 120)
}
}
@Suite("截图")
struct ScreenshotTests {
@Test @MainActor func shotMatchesExpectedWidth() throws {
let renderer = ImageRenderer(content: QualityBoard())
renderer.scale = 2
let image = try #require(renderer.cgImage)
#expect(image.width == 680)
let url = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("shots/ios-quality-board.png")
guard !FileManager.default.fileExists(atPath: url.path) else { return }
let data = try #require(
NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:])
)
try FileManager.default.createDirectory(
at: url.deletingLastPathComponent(),
withIntermediateDirectories: true
)
try data.write(to: url)
}
}