Dan Morrow
Dan Morrow

Reputation: 4481

How to test a Swift Package, that uses UIKit?

I've created my first Swift Package recently. It's only used in iOS currently, and if it goes anywhere else, it might be on tvOS, but that would be it.

I was having trouble getting UIKit to be used. And I saw this note over here, and that really helped solve my problem: https://stackoverflow.com/a/58684636/1435520

The command I used (and mentioned above) is this:

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

However, I'd really like to have unit tests. Running swift test... with the same arguments mentioned for swift build. But then that gives me a new error:

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

So, I guess I'm just wondering if it's possible to do this. Like, how could I create a Swift Package, that uses UIKit, and have it be testable?

Upvotes: 9

Views: 4043

Answers (3)

Kamil Zaborowski
Kamil Zaborowski

Reputation: 366

I have spent a lot of time trying to run tests of some complex framework. None of the solutions have worked for me.

I found out that generate-xcodeproj is marked as deprecated. The reason to that I have found here.

"... starting Xcode 11, opening and building packages is directly supported by Xcode ..."

So I run xcodebuild test directly in the package folder, without creating the xcodeproj.

xcodebuild -scheme '<package name>-Package' -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPad Air (4th generation),OS=15.0' test

And it have worked.

Upvotes: 5

Hadi
Hadi

Reputation: 1363

I was trying to build and test my internal packages on CI, nothing works for me, and generating an xcodeproj will be deprecated in the future.

What works for me is to just specify the target as macOS 10.15 by doing this without anything else:

swift test -Xswiftc -target -Xswiftc x86_64-apple-macosx10.15

Upvotes: -1

edbentley
edbentley

Reputation: 331

I've been using the command xcodebuild as detailed in this forum post, which requires you to also generate a .xcodeproj:

swift package generate-xcodeproj
xcodebuild build -sdk iphoneos -scheme 'MyPackage-Package'
xcodebuild test -destination 'name=iPhone 11' -scheme 'MyPackage-Package'

But if you're using SPM Resources, you'll run into issues as I detailed here: https://bugs.swift.org/browse/SR-13773

Upvotes: 5

Related Questions