# \[RFC\] Attachment lifetimes

**URL:** <https://forums.swift.org/t/rfc-attachment-lifetimes/81665>\
**Category:** Swift Testing\
**Tags:** api, swift-testing\
**Created:** [August 15, 2025, 3:46pm UTC](https://forums.swift.org/t/rfc-attachment-lifetimes/81665 "2025-08-15T15:46:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grynspan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/grynspan/32/15315_2.png) [@grynspan](https://forums.swift.org/u/grynspan)\
**Post date:** [August 15, 2025, 3:46pm UTC](https://forums.swift.org/t/rfc-attachment-lifetimes/81665/1 "2025-08-15T15:46:59Z")

</div>

Hello Swift enthusiasts! I'm looking for feedback from you all about a future enhancement to attachments in Swift Testing. Read on!

We introduced [attachments](https://github.com/swiftlang/swift-evolution/blob/main/proposals/testing/0009-attachments.md) to Swift Testing in Swift 6.2. This feature allows you to create a file containing data relevant to a test. In **XCTest** on Apple platforms, you can also specify the _lifetime_ of an attachment by setting the [`lifetime`](https://developer.apple.com/documentation/xctest/xctattachment/lifetime-swift.property) property of an `XCTAttachment` object. This lets you avoid serializing and saving an attachment if its data won't be necessary (i.e. "if the test passes, don't bother saving this attachment.")

We didn't initially include this feature in Swift Testing so we could keep the scope of the feature narrow, but we're now looking at implementing it for an upcoming Swift release. So my question to the community is: if you could tell Swift Testing to only record an attachment to a test under certain conditions, what would those conditions be?

For example (these are not the only possible answers!):

- Always record this attachment
- Record this attachment _only if_ the current test fails
- Record this attachment _only if_ the current test passes
- etc.

---

<div class="post-metadata">

**Author:** ![smontgomery](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/smontgomery/32/965_2.png) [@smontgomery](https://forums.swift.org/u/smontgomery)\
**Post date:** [August 15, 2025, 4:02pm UTC](https://forums.swift.org/t/rfc-attachment-lifetimes/81665/2 "2025-08-15T16:02:13Z")

</div>

One use case could factor in issue **severity** , which was a concept introduced in [ST-0013: Test Issue Severity](https://github.com/swiftlang/swift-evolution/blob/main/proposals/testing/0013-issue-severity-warning.md). As an example,

- Attach if any issues with `warning` (or higher) severity were recorded.

This would be useful because sometimes an attachment may help track down the reason why a warning-severity issue was recorded.

---

<div class="post-metadata">

**Author:** ![younata](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/younata/32/34709_2.png) [@younata](https://forums.swift.org/u/younata)\
**Post date:** [August 19, 2025, 9:41pm UTC](https://forums.swift.org/t/rfc-attachment-lifetimes/81665/3 "2025-08-19T21:41:43Z")

</div>

I love this. Yes to all of those options + Stuart’s suggestion of severity-based.

Maybe as an escape hatch, a custom option that evaluates some passed-in closure with the attachment and current state of the test?

---

<div class="post-metadata">

**Author:** ![grynspan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/grynspan/32/15315_2.png) [@grynspan](https://forums.swift.org/u/grynspan)\
**Post date:** [August 19, 2025, 9:54pm UTC](https://forums.swift.org/t/rfc-attachment-lifetimes/81665/4 "2025-08-19T21:54:28Z")

</div>

> [@younata](#):
>
> Maybe as an escape hatch, a custom option that evaluates some passed-in closure with the attachment and current state of the test?

We will likely need to evaluate this property after the test has ended, so it's probably not practical for us to try to encode any "current state", but a closure with barebones info like:

```swift
case custom(
  _ filter: @Sendable (
    _ attachment: borrowing Attachment<T>,
    _ testPassed: Bool
  ) -> Bool
)

```

Might be feasible.

---

<div class="post-metadata">

**Author:** ![grynspan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/grynspan/32/15315_2.png) [@grynspan](https://forums.swift.org/u/grynspan)\
**Post date:** [October 6, 2025, 7:49pm UTC](https://forums.swift.org/t/rfc-attachment-lifetimes/81665/5 "2025-10-06T19:49:18Z")

</div>

Pitch thread is up [here](https://forums.swift.org/t/pitch-conditionally-saving-attachments-aka-attachment-lifetimes/82541).
