简体   繁体   English

如何使用 `swift test` 对 iOS package 进行单元测试?

[英]How to unit test iOS package with `swift test`?

I have an iOS library with Swift package manager.我有一个 iOS 图书馆和 Swift package 经理。 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.我正在尝试使用swift test运行单元测试我不使用xcodebuild的原因是,我试图从我的源代码管理中删除.xcodeproj Also, there is a warning when we create xcodeproj with spm, that generate-xcodeproj will be deprecated soon.此外,当我们使用 spm 创建xcodeproj时,会有一个警告,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. TL;DR 这个库依赖于 Lottie 我试过在根目录下运行swift test ,但是它给出了很多错误。 like /.build/checkouts/lottie-ios/lottie-swift/src/Public/Animation/AnimationView.swift:859:11: error: cannot find 'superview' in scope I tried像/.build/checkouts/lottie-ios/lottie-swift/src/Public/Animation/AnimationView.swift:859:11: error: cannot find 'superview' in scope 我试过了

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 .然后它给出错误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我没有在任何地方指定 macos10.15,我在 macOS 11.5.1 上

I can build the package using the above command by replacing test with build .我可以使用上述命令通过将test替换为build来构建 package 。 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 ?据我了解,这是因为我没有指定目标模拟器,是否可以通过swift test指定目标模拟器?

swift test only works for macOS testing. swift test仅适用于 macOS 测试。 It does not support cross builds for iOS.它不支持 iOS 的交叉构建。

You do not need an existing Xcode project to use xcodebuild .您不需要现有的 Xcode 项目即可使用xcodebuild Run the command, xcodebuild -list to initialize a Package.swift file for testing.运行命令xcodebuild -list以初始化Package.swift文件以进行测试。

See the example here .请参阅此处的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM