【问题标题】:Sharing code across test targets when using the Swift Package Manager使用 Swift 包管理器时跨测试目标共享代码
【发布时间】:2020-12-22 06:54:12
【问题描述】:

在使用 Swift 包管理器时,我需要在测试目标之间共享一些代码。为此,我有一个 .testTarget,我还将它命名为另一个 .testTarget 中的依赖项。

这是一个简单的例子:

// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "ExampleLib",
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "ExampleLib",
            targets: ["ExampleLib"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "ExampleLib",
            dependencies: []),
        .testTarget(
            name: "Common",
            dependencies: ["ExampleLib"]),
        .testTarget(
            name: "ExampleLibTests",
            dependencies: ["Common"]),
            
    ]
)

如果我尝试在 Xcode 中构建这个包,我会收到以下错误:

Unable to resolve build file: XCBCore.BuildFile (The workspace has a reference to a missing target with GUID 'PACKAGE-TARGET:Common')

但是,如果我从命令行 (swift build) 构建或从命令行 (swift test) 测试,我会成功。

我正在使用 Xcode 12 beta 6,但也尝试过 Xcode 11.5(更改了 Package.swift 标头)并获得了相同的结果。

这是完整的 Swift 包示例: https://www.dropbox.com/s/h6ypvbfonnb2zyk/ExampleLib.zip?dl=0

我真的很想在 Xcode 中使用它来构建 iOS。想法?

【问题讨论】:

    标签: swift xcode swift-package-manager


    【解决方案1】:

    我遇到了同样的问题,并通过为Common 定义一个目标(不是测试目标)来解决它。

    我的Package.swift 文件:

    // ...
    .target(name: "Common", dependencies: ["App"], path: "Tests/Common"),
    .testTarget(name: "AppTests", dependencies: ["App", "Common"]),
    .testTarget(name: "IntegrationTests", dependencies: ["App", "Common"])
    // ...
    

    【讨论】:

    • 摇滚!谢谢!有用!我假设这不是预期的行为,并且可以解决?
    猜你喜欢
    • 2022-06-13
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多