How to unit test iOS package with `swift test`?

Viewed 914

I have an iOS library with Swift package manager. I am trying to run unit tests with swift test The reason why I am not using xcodebuild is, I am trying to remove the .xcodeproj from my source control. Also, there is a warning when we create xcodeproj with spm, that generate-xcodeproj will be deprecated soon. TL;DR This library depends on Lottie I tried just running swift test in the root directory, but it gives lot of errors. like /.build/checkouts/lottie-ios/lottie-swift/src/Public/Animation/AnimationView.swift:859:11: error: cannot find 'superview' in scope I tried

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

Then it gives the error error: module 'XCTest' was created for incompatible target x86_64-apple-macos10.15. I did not specify macos10.15 anywhere, I am on macOS 11.5.1

I can build the package using the above command by replacing test with build. What is wrong with my test command?

As I understand this is because I am not specifying the target simulator, is it possible to specify the target simulator with swift test?

1 Answers

swift test only works for macOS testing. It does not support cross builds for iOS.

You do not need an existing Xcode project to use xcodebuild. Run the command, xcodebuild -list to initialize a Package.swift file for testing.

See the example here.

Related