I made a little SwiftPM Package that uses UIKit. How can I run tests from command line

Hello swift community

I am trying to decompose my big iOS app into small packages. In the process of it, I found some problems when trying to test SPM with iOS specific (UIKit) stuff in it.

I have made one small SPM Package library and added it as a local package into my project. I see that now an additional scheme is available in my Xcode project.

I can successfully build via Xcode UI and also with command line:

xcodebuild -scheme MyLittleViewUtil build -destination "platform=iOS Simulator,name=iPhone 8,OS=latest"

xcodebuild -scheme MyLittleViewUtil test -destination "platform=iOS Simulator,name=iPhone 8,OS=latest"

Questions:

  1. What is the correct command to run something similar to above but from SPM command line?

For building this works:

swift build -Xswiftc "-sdk" -Xswiftc "`xcrun --sdk iphonesimulator --show-sdk-path`" -Xswiftc "-target" -Xswiftc "x86_64-apple-ios14.0-simulator"

But for testing it fails:

swift test -Xswiftc "-sdk" -Xswiftc "`xcrun --sdk iphonesimulator --show-sdk-path`" -Xswiftc "-target" -Xswiftc "x86_64-apple-ios14.0-simulator"

error: module 'XCTest' was created for incompatible target x86_64-apple-macos10.15:

  1. My little library uses UIKit and some UIViews. I have heard that for now SPM tests cannot run on an host application. Is there anything I can do to test the UI from SPM only? Is Xcode13/Swift/SPM functionality enhanced in anyway that alleviate my situation? What are my alternatives?

Any help is appreciated.

xcodebuild can build/test SwiftPM packages directly, without an Xcode project.

In your package directory, run xcodebuild -list to see the available "schemes" for your package.

Then run xcodebuild as if the package were an Xcode project:

xcodebuild -scheme MyLittleViewUtil test -destination "platform=iOS Simulator,name=iPhone 8,OS=latest"
5 Likes

Thank you. I didn't know that command was ok without an Xcode project. !
Do you know if there is an equivalent swift test ... command that at least compiles and try to run the tests? (I know that probably not all tests will succeed because I don't have a proper UIView in hierarchy etc)

There is no equivalent command. Raw SwiftPM only officially supports builds for the host platform. It has no notion of simulators or separate devices. xcodebuild is the correct tool for the job.

2 Likes

Unfortunately this doesn't work if the same folder containing Package.swift also has an Xcode project with schemes. xcodebuild -list only picks ups those schemes and not any additional targets in the package.

If the Package.swift and the Xcode project are in the same folder but you also have them added to the same workspace you can use the -workspace option and it will list the schemes for both:

xcodebuild -workspace MyProject.xcworkspace -list
1 Like