[Pitch] Revise Swift Testing's Attachment/Encodable interop

Swift Testing provides automatic support for attaching values that conform to Encodable and NSSecureCoding if they also conform to Attachable. But that extra Attachable conformance is really just there to silence the compiler, and it doesn't allow test authors to use custom encoders.

I propose revising the API for attaching Encodable and NSSecureCoding values. Given the following type:

struct HotDog: Encodable { ... }

Then, instead of something like this:

extension HotDog: @retroactive Attachable {}

@Test func `Add condiments to hot dog`() {
  let hotDog = ...
  ...
  Attachment.record(hotDog, named: "hot dog.json")
}

You'll now write:

@Test func `Add condiments to hot dog`() throws {
  let hotDog = ...
  ...
  let attachment = try Attachment(encoding: hotDog, as: .json)
  // OR: try Attachment(encoding: hotDog, using: FoodEncoder())
  Attachment.record(attachment)
}

Read the full proposal here.

4 Likes