Chris Prince
Chris Prince

Reputation: 7564

Test-only Dependencies When Using the Swift Package Manager

I've seen mentions of test-only dependencies when using the Swift package manager but haven't been able to get them to work. For example:

https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160104/005409.html https://github.com/apple/swift-package-manager/pull/74 http://blog.krzyzanowskim.com/2016/08/09/package-swift-manual/#testDependencies

What I want is to have the dependency used in my XCtests (e.g., for a specific target), but not applied in the deployed package.

Pointers to working examples would be appreciated.

Upvotes: 4

Views: 2146

Answers (3)

kakaiikaka
kakaiikaka

Reputation: 4477

Updates: SE-0226 mentions that it is partially supported.

As of Swift5.9, it is fully supported.

This is a package manifest from one of my SPM. I need to test the WebDAV feature in the test cases only.

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

import PackageDescription

let package = Package(
    name: "FilesProvider",
    platforms: [
        .iOS(.v13),
        .tvOS(.v13),
        .macOS(.v10_15),
        .visionOS(.v1)
    ],
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
            name: "FilesProvider",
            targets: ["FilesProvider"]
        )
    ],
    dependencies: [ 
        .package(path: "../GCDWebServer")
    ],
    targets: [
        .target(name: "FilesProvider",
                dependencies: [],
                path: "Sources"
        ),
        .testTarget(name: "FilesProviderTests",
                dependencies: ["FilesProvider", 
                    .product(name: "GCDWebDAVServer",package: "GCDWebServer")
                ],
                path: "Tests"
        ),
    ]
)

And Yes, it works like a charm:

enter image description here

Upvotes: 1

Lvsti
Lvsti

Reputation: 1525

@Vadim's answer is correct, there is currently no official API for test dependencies, but there are workarounds. ReactiveSwift for example uses an alternate Package.swift for tests that they overwrite the original one with when executing tests on the CI (see their travis config file). It's not elegant but well, it does the job until SPM brings back this highly needed feature.

Upvotes: 3

Vadim Eisenberg
Vadim Eisenberg

Reputation: 3427

Currently "Test-only dependencies" feature is disabled in the Swift Package Manager. It was implemented initially, but was removed in this commit.

From the commit's description:

Remove testDependencies from PackageDescription This feature was supposed to support dependencies only for the root package but at some point it stopped working however the API still remained open. This patch removes the public API. This is a valid and desired feature which is supposed to come back after it goes through proper review on swift evolution.

Upvotes: 8

Related Questions