This framework provides powerful tools for inspecting SwiftUI views in unit tests. It allows developers to examine the structure of their views, access individual elements, and simulate user interactions, all within a testing environment.
- View Structure Inspection: Examine the hierarchy and composition of SwiftUI views.
- Element Access: Easily access specific elements within a view for verification.
- Interaction Simulation: Simulate user interactions like taps, toggles, and text input.
- State Inspection: Verify the state of views and their subcomponents.
- Modifier Inspection: Examine and test view modifiers like
onAppear
,task
, and custom modifiers.
Add the following to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/sisoje/swiftui-view-inspection.git", from: "1.0.0")
]
Then, import the framework in your test files:
import XCTest
import SwiftUI
@testable import ViewInspection
To inspect a view:
let view = Text("Hello, World!")
let snapshot = view.snapshot
XCTAssertEqual(snapshot.oneElement(TestElement.View._Text.self).string, "Hello, World!")
To test a button tap:
var b = false
Button("") { b = true }.snapshot.oneElement(TestElement.View._Button.self).tap()
XCTAssertEqual(b, true)
To test an task
modifier:
var x = 0
await EmptyView().task { x = 1 }.snapshot.oneElement(TestElement.Modifier._Task.self).doTask()
XCTAssertEqual(x, 1)
- Reflection-based Inspection: Uses Swift's reflection capabilities to inspect view structures.
- Type-safe Element Access: Strongly-typed access to view elements and modifiers.
- Comprehensive SwiftUI Support: Covers a wide range of SwiftUI components and modifiers.
Contributions are welcome! Please follow these steps:
- Fork the repository and create your branch from
main
. - Add or update tests for any new or changed functionality.
- Ensure your code follows the existing style and passes all tests.
- Create a pull request with a clear description of your changes.
This project is licensed under the MIT License - see the LICENSE file for details.