Skip to content

Conversation

ajkolean
Copy link

This PR adds support for recording snapshot failure artifacts as Swift Testing attachments while maintaining existing XCTest behavior.

Changes

  • Introduced STAttachments helper to conditionally call Swift Testing’s Attachment.record when available (canImport(Testing) and compiler(>=6.2)).
  • Updated image snapshotting (UIImage, NSImage) to bundle raw PNG data alongside XCTAttachments so they can be re-recorded under Swift Testing.
  • Modified verifySnapshot:
  • Attaches reference snapshots when recording.
  • Attaches failure/difference artifacts within Swift Testing context.
  • Falls back to existing XCTContext attachments for XCTest.
  • Added new SwiftTestingAttachmentTests suite to verify attachment behavior for both text and image snapshots.
  • Added Documentation/SwiftTestingAttachments.md explaining feature, usage, and requirements.

Why this matters

Adds richer diagnostics for snapshot failures directly in Xcode’s native Swift Testing UI without altering behavior for XCTest users.

Related discussion

See GitHub Discussion #909: “No screenshots attachments in Swift Testing tests” — this PR implements the solution requested there.

Let me know if you want it tweaked further!

- Create DualAttachment to bridge XCTest and Swift Testing attachments
- Store attachment data alongside XCTAttachments for Swift Testing
- Record attachments via Swift Testing's Attachment API when available
- Add three separate attachments for string diffs (reference, actual, diff)
- Optimize large image attachments with automatic compression

Addresses pointfreeco#909
- Keep string snapshots with single patch attachment (original behavior)
- Only image snapshots create three attachments (reference, failure, difference)
- Focus on adding Swift Testing support without changing existing attachment patterns
- Add comprehensive tests to verify attachment creation
- Update SwiftTestingAttachmentTests to use withKnownIssue properly for expected failures
- Add compiler conditional for inline snapshot test to handle Swift 6.2+ error message format changes
- Remove unnecessary @_spi annotations from UIImage snapshots
- Update documentation formatting to match existing patterns
- Ensure all tests pass with expected known issues
- Match existing codebase pattern using NSLock with @unchecked Sendable
- Replace DispatchQueue.sync with lock.lock()/defer { lock.unlock() }
- Consistent with Counter class implementation in AssertSnapshot.swift
- Add STAttachments helper to centralize Swift Testing attachment recording
- Add proper platform guards matching project conventions for XCTest imports
- Simplify nested #if conditions using && operator
- Add UUID-based storage for reliable attachment retrieval
Replace brittle AttachmentStorage with direct data embedding in XCTAttachment.userInfo. This eliminates:
- Global storage with ObjectIdentifier keys that could collide
- Thread synchronization overhead
- Complex UUID tracking

The new approach embeds image data directly in XCTAttachment's userInfo dictionary, making the data flow direct and collision-free without changing any public APIs.
@ajkolean
Copy link
Author

Hey @mbrandonw just noticed you had this branch a while back:
https://github.com/pointfreeco/swift-snapshot-testing/tree/swift-testing-attachments

Feel free to use this PR as a reference if helpful. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant