In ST-0014, we introduced the ability to attach an image to a test in Swift Testing and have the testing library automatically serialize it to a PNG, JPEG, or other image format. This proposal was intentionally limited to Apple platforms and Apple image types because image serialization is inherently platform-specific.
The natural next step for this feature is to extend it to other platforms. The next platform I propose adding support for is Windows:
import Testing
import WinSDK
@MainActor @Test
func `attaching an HBITMAP`() throws {
let bitmap: HBITMAP = ...
defer {
DeleteObject(bitmap)
}
Attachment.record(bitmap, named: "GDI bitmap", as: .png)
}
@MainActor @Test
func `attaching an IWICBitmapSource`() throws {
let bitmap: UnsafeMutablePointer<IWICBitmapSource> = ...
defer {
bitmap.lpVtbl.Release(bitmap)
}
Attachment.record(bitmap, named: "WIC bitmap", as: .jpeg)
}
Read the full proposal here.
Trying it out
To try this feature out, add a dependency to the main branch of swift-testing to your project:
...
dependencies: [
...
.package(
url: "https://github.com/swiftlang/swift-testing.git",
branch: "main"
),
]
Then, add two target dependencies to your test target:
.testTarget(
...
dependencies: [
...
.product(name: "Testing", package: "swift-testing"),
.product(name: "_Testing_ExperimentalImageAttachments", package: "swift-testing"),
]
Add the following imports to any source file that will attach an image to a test:
import WinSDK
@_spi(Experimental) import _Testing_WinSDK
Note: This extra product dependency and extra
importstatement are a side effect of how packages are built. They will not be required in the final implementation of this feature.