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")

Where is Bundle.module defined?

@JetForMe

Where is Bundle.module defined?

From Access a resource in code:

When you build your Swift package, Xcode treats each target as a Swift module. If a target includes resources, Xcode creates a resource bundle and an internal static extension on Bundle to access it for each module. Use the extension to locate package resources. For example, use the following to retrieve the URL of a property list you bundle with your package:

let settingsURL = Bundle.module.url(forResource: "settings", withExtension: "plist")

Always use Bundle.module when you access resources. A package shouldn’t make assumptions about the exact location of a resource.

If you want to make a package resource available to apps that depend on your Swift package, declare a public constant for it. For example, use the following to expose a property list file to apps that use your Swift package:

let settingsURL = Bundle.module.url(forResource: "settings", withExtension: "plist")

When I try it, I get "'module' is inaccessible due to 'internal' protection level". I do see that it's being generated for one of my project's dependencies (in this case, it's a Vapor project, and the module that code completion jumps to is in in swift-crypto’s DerivedSources folder).

UPDATE: Seems I had a malformed Package.swift (a missing comma), and instead of showing the error, Xcode just choked on compiling my test code. Running swift-test on the command line revealed this.

Using resources with Swift Testing
 
 
Q