8

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?

Dan Morrow
  • 4,433
  • 2
  • 31
  • 45
  • Could you rig up a sample? – Jon Reid Sep 06 '20 at 19:42
  • @JonReid I have an example of a similar problem, with my [RudifaUtilPkg](https://github.com/rudifa/RudifaUtilPkg). The master branch contains no UIKit dependencies yet, and the workflow action `build_and_test` executes successfully. The new branch `rf-attributed-strings` depends on UIKit, and the action `build_and_test` fails (see [pull request #5](https://github.com/rudifa/RudifaUtilPkg/pull/5)) test details). – rudifa Oct 19 '20 at 19:24
  • 1
    On my home mac I have been trying possible solutions along the lines mentioned by @DanMorrow, and I hit the same problem: ``swift build -Xswiftc "-sdk" -Xswiftc "`xcrun --sdk iphonesimulator --show-sdk-path`" -Xswiftc "-target" -Xswiftc "x86_64-apple-ios14.0-simulator"`` succeeds while ``swift test -Xswiftc "-sdk" -Xswiftc "`xcrun --sdk iphonesimulator --show-sdk-path`" -Xswiftc "-target" -Xswiftc "x86_64-apple-ios14.0-simulator"`` fails with `... was created for incompatible target x86_64-apple-macos10.15`. Any helpful suggestions would be welcome. – rudifa Oct 19 '20 at 19:34

3 Answers3

5

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

edbentley
  • 191
  • 10
5

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.

Josh Brown
  • 52,385
  • 10
  • 54
  • 80
-1

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
Hadi
  • 1,294
  • 1
  • 8
  • 8
  • This does not work for me because I'm using UIKit and I get the error `error: no such module 'UIKit'`. In this other question https://stackoverflow.com/a/71382981/3012842 they say it's not possible to use "swift test" for iOS. – Jose Celano Jun 23 '22 at 15:09