talira

ViewInspector

Unit testing for SwiftUI views by inspecting the view hierarchy at runtime

open sourceMITfree

ViewInspector is a Swift library for unit testing SwiftUI views. It walks a view hierarchy at runtime with Swift's official reflection API, so a test can find views by type or condition, read standard views' contents and attributes (a Text's string in a given locale, its font), get a custom view's actual state (@State, @Binding, @ObservedObject, @EnvironmentObject), and trigger callbacks such as a button tap or onAppear, with helpers for asynchronous tests. It goes in the unit-test target only.

Problems it solves

  • write unit tests that check what my SwiftUI view actually shows
  • tap a button in a SwiftUI view from an XCTest without running UI tests
  • assert on the state inside a SwiftUI view in a unit test

Features

  • find views by type, text or condition in a SwiftUI hierarchy
  • read standard views' values and attributes
  • access a custom view's state, bindings and observed objects
  • tap buttons and call onAppear and other callbacks from tests