Using resources with Swift Testing

Hey guys.

I’m working on a project where I’m using the SwiftTesting framework instead of XCTest to run my unit tests. I have a file (test.png) located in my test resources folder:

PackageName > Tests > PackageNameTests > Resources > test.png

I’m trying to access this file in my tests, but I’m running into issues when trying to load it dynamically. Here’s what I’ve tried so far:

Using Bundle.module.path(forResource:ofType:): This approach didn’t work, as Bundle.module seems to be unsupported or returns nil in Swift Testing.

Using #file Macro for Relative Paths: I tried constructing a path based on #file and navigating to the resources directory, but it also didn’t yield the correct path.

Has anyone successfully loaded test resources in the Swift Testing framework? Is there a recommended way to access resource files in Swift Testing, especially for projects where Bundle.module isn’t available?

I've gone through the Apple Docs for Swift Testing, but I can't seem to find anything that answers my question.

Thanks in advance guys!

Just putting in an update here for anyone who stumbles upon this post and has a similar issue.

This is resolved by adding this entry into your Package.swift file:

.testTarget(
    name: "PackageNameTests",
    dependencies: ["PackageName"],
    resources: [
        .copy("Resources/test.png")
    ]
)

After that's all synced, you can use the below code to access the correct path to access the resource:

Bundle.module.path(forResource: "test", ofType: "png")
Accepted Answer

Just putting in an update here for anyone who stumbles upon this post and has a similar issue.

This is resolved by adding this entry into your Package.swift file:

.testTarget(
    name: "PackageNameTests",
    dependencies: ["PackageName"],
    resources: [
        .copy("Resources/test.png")
    ]
)

After that's all synced, you can use the below code to access the correct path to access the resource:

Bundle.module.path(forResource: "test", ofType: "png")
Using resources with Swift Testing
 
 
Q