I'm building a Swift package that works with UIKit code intended to be used with iOS applications. It will have views that will need to be tested. From what I've seen, you can't just test views in a vanilla Swift package. There needs to be a iOS Xcode project to enable testing the UIKit code.This is fine. The trouble is I want two things that appear to be mutually exclusive. The first is I want the Swift package to exist in the same Git repo as that which houses its tests. The second is I want to be able to use the Swift package as any other Swift package i.e. be able to add it to other Xcode projects as a Swift dependency.
Is there a way to have a Swift package intermingled with an Xcode project and still be able to vend the Swift package as a library as you would with any other Swift package?
Just put the
Package.swift at the repository root, and then reference it as a local dependency from an Xcode project anywhere in the repository. (Drag the repository root from Finder into the Xcode project’s file list).
This is what ended up working for me:
- Create Swift package as you normally would.
- Create a directory named "Xcode" in the root of the Swift package.
- Create an iOS "App" project inside the "Xcode" directory.
- Close Xcode window hosting your new iOS project if one is opened.
- Navigate to the "Xcode" directory in Finder, and drag and drop the
.xcodeproj into the root of the Swift package within Xcode.
- Now go to "New Scheme" (right click the scheme at the top of the window). The way it worked for me is — on clicking "New Scheme" it was already asking me if I wanted to create it for "App" (I called my iOS project "App"). Then I was able to build the iOS project.
- Now in order for the tests to run, engage the new iOS scheme, and click "Edit Scheme". Go to the "Test" tab. Hit the "+" button at the bottom, and add your iOS project's UI tests target (I deleted the other test target intent on using the Swift package's "Tests" directory for no UI test).
After this I was able to go into the "AppUITests" target, execute the default test function, and they would run successfully. If they don't for you, make sure you have an actual device set as the run destination.