Swiftpack.co - Package - nalexn/ViewInspector

ViewInspector for SwiftUI

Platform Build Status codecov

ViewInspector is a library for unit testing SwiftUI views. It allows for traversing a view hierarchy at runtime providing direct access to the underlying View structs.


SwiftUI views are a function of state. We can provide the input, but couldn't verify the output. Until now!

Helpful links

Use cases

1. Verify the view's inner state

You can dig into the hierarchy and read the actual state values on any SwiftUI View:

func testVStackOfTexts() throws {
    let view = VStack {
    let text = try view.inspect().vStack().text(2).string()
    XCTAssertEqual(text, "3")

Text value inspection supports various formatting styles, text attributes, and localization.

2. Trigger side effects

You can simulate user interaction by programmatically triggering system-controls callbacks:

let button = try view.inspect().hStack().button(1)
try button.tap()

let list = try view.inspect().list()
try list[5].view(RowItemView.self).callOnAppear()

The library provides helpers for writing asynchronous tests for views with callbacks.

3. Extract custom views from the hierarchy of any depth

It is possible to obtain a copy of your custom view with actual state and references from the hierarchy of any depth:

let sut = try view.inspect().tabView().navigationView()

The library can operate with various types of the view's state, such as @Binding, @State, @ObservedObject and @EnvironmentObject.


Which views and modifiers are supported?

Check out the detailed list. There is currently almost full support for SwiftUI 1.0 API, the 2.0 support is under active development.

You are welcomed to contribute! Use po Inspector.print(view) as AnyObject in lldb for printing the inner view structure - this is the tool that would help you dig super fast!

Is it using private APIs?

ViewInspector is using official Swift reflection API to dissect the view structures.

So this framework is production-friendly for the case if you accidentally (or intentionally) linked it with the build target.

How do I add it to my Xcode project?

Swift Package Manager

  1. In Xcode select File ⭢ Swift Packages ⭢ Add Package Dependency...
  2. Copy-paste repository URL: https://github.com/nalexn/ViewInspector
  3. Hit Next two times, under Add to Target select your test target. There is no need to add it to the build target.
  4. Hit Finish


github "nalexn/ViewInspector"


pod 'ViewInspector'

How do I use it in my project?

Please refer to the Inspection guide. You can also check out my other project that harnesses the ViewInspector for testing the entire UI.

Other questions, concerns or suggestions?

Ping me on Twitter or just submit an issue or a pull request on Github.

blog venmo


Stars: 549


Used By

Total: 0