Swift packages with UIKit code and testing

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:

  1. Create Swift package as you normally would.
  2. Create a directory named "Xcode" in the root of the Swift package.
  3. Create an iOS "App" project inside the "Xcode" directory.
  4. Close Xcode window hosting your new iOS project if one is opened.
  5. Navigate to the "Xcode" directory in Finder, and drag and drop the .xcodeproj into the root of the Swift package within Xcode.
  6. 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.
  7. 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.