I'm currently writing a macro which outputs Swift Testing code:
// Macro code ...
@MainActor
@SnapshotSuite
struct MySuite {
func makeView() -> some View {
Text("a view")
}
}
which expands to...
// Expanded macro code ...
@MainActor
@Suite
struct _GeneratedSnapshotSuite {
@MainActor
@Test(.tags(.snapshots))
func assertSnapshotMakeView() async throws {
let generator = SnapshotGenerator(
testName: "makeView",
traits: [
.theme(.all),
.sizes(devices: .iPhoneX, fitting: .widthAndHeight),
.record(false),
],
configuration: .none,
makeValue: {
MySuite().makeView()
},
fileID: #fileID,
filePath: #filePath,
line: 108,
column: 5
)
await __assertSnapshot(generator: generator)
}
}
In short, this macro creates snapshot tests from a function.
This all works but I'm finding a couple of limitations, potentially with how Macros are expanded in Swift.
- Xcode diamonds are not visible
- The
snapshots
tag isn't discovered
There are a couple of things worth noting though:
- The suites and tests are discovered in Xcode's Test Navigator each time Xcode runs tests (
cmd
+u
) - although they need to rerun to be updated. - I manually add a
@Suite
in my code as well as my@SnapshotSuite
to all of the suites. - (The tags never seem to be available though)
Couple of questions on this:
- Is this a known issue?
- Are there any workarounds?
I do wonder if there's a workaround for showing the diamonds with XCTest
APIs such as: