Testing SPM dependencies targets

Hi there,
I'm quite new to PackageManager and I was wondering, is there a way to test the XCTest cases included in my SPM dependencies, from the main project test target?
Meaning, testing my main project should also trigger my dependencies' tests.
(by main project i mean the top-most project that uses my SPM libraries)

I've been creating SPM kits like so:

// swift-tools-version:5.3
import PackageDescription

let package = Package(
    name: "myKit",
    platforms: [.iOS("14.5.0")],
    products: [
        .library(name: "myKit", targets: ["myKit"])
        //.library(name: "myKitTests", targets: ["myKitTests"])  // tried this, i get a warning that says this duplicated target is ignored
    ],
    dependencies: [
        // My kit's own dependencies. Their own tests i don't mind if i can't access them.
        ...
    ],
    targets: [
        .target(name: "myKit", dependencies: [...]),
        .testTarget(name: "myKitTests", dependencies: ["myKit"])
    ]
)

This kit is imported in my main project, but i can't exectute myKits' tests from there.
I've declared all kits' tests as public but not any better.

What is even more frustrating is that i can see my test files in the "Package Dependencies" section of the Project navigator and importing my kit as a local SPM library (which i don't want) allows me to trigger the kit's tests.

Any idea of how i could accomplish this?
Many thanks

1 Like

The standard method would be to download and open the dependency itself and run them from there.

Even if a dependency were theoretically released in a state where tests were failing, there is nothing you could do to fix the release anyway. It is only if you have overridden the dependency with a local copy and have make changes that there would be any reason to see if said changes broke anything. Running tests over and over on something that cannot have changed would usually just be a waste of time.

If you do have some unusual use for it that I cannot think of, then I would suggest putting your XCTestCase subclasses in a real library, marking them as open and then merely doing class InheritorTests: MyKitTests {} in the test target. That way your test target will inherit all the tests, and any other module that wishes can depend on and import that test library to inherit them the same way.

1 Like

There is no way to do it currently, I have asked for this in the past, although I can not find the discussion anymore. But it should be possible to write a plugin for SPM that will walk through the dependencies and test them one by one.

I for one will use it in my CI heavily.

Here is a brief post outlining where we are: Plugins for Swift Package Manager – available from Swift 5.6

just my .2 cents,
Martin

1 Like

IIUC via Xcode you can do it by adding the test target to your scheme test action. Then command+U would test both.

Thank you all for your replies.

That's the thing. I should have given more context. I do have control on most if not all of the kits I want to test. I would like to know if I or my team breaks some code on the kits without multiplying CI processes.
I will most certainly try and inherit my dependencies' test cases like you suggested though. Thanks for the tip.

That's a shame. I will do some research on SPM plugins and give it a try also. Thanks!

There's the problem. I cannot access those test targets and add them to my scheme.