How do you UI test a swift package?

Some swift packages have UI components which you often want to UI test. If I add a test file like this:

import XCTest

class MobileHostUITests: XCTestCase {

    func testUI() {
        let app = XCUIApplication()
        app.launch()
    }
}

I get this result when I run swift test:

**2021-06-24 13:56:57.681638+0100 xctest[44498:968443] *** Assertion failure in -[XCUIApplication init], XCUIApplication.m:91**

**MobileHostUITests.swift:15: error: -[MobileHostUITests testUI] : No target application path specified via test configuration: <XCTestConfiguration: 0x7fcd96335330>**

What's the procedure for UI testing a swift package?

UI testing is generally going to require an app to proxy with the XCUIApplication.

Common strategies here are to make a "runner" app that only exists to show the UI or to only do UI testing on the app that consumes the package.

1 Like

You must add the package to a project with a UI app and then add a UI test target to the project.

Without a UI app to run what would you expect to happen in this line? What app would be launched? XCTest is not going to create an app for you. That just wouldn't make sense.

You could probably construct a single shell app that loads in different components as the root view based on a launch flag. You can then configure the launch flags in your test.

Terms of Service

Privacy Policy

Cookie Policy