# \[Pitch\] Conditionally saving attachments (AKA attachment lifetimes)

**URL:** <https://forums.swift.org/t/pitch-conditionally-saving-attachments-aka-attachment-lifetimes/82541>\
**Category:** Pitches\
**Tags:** api, swift-testing\
**Created:** [October 6, 2025, 7:48pm UTC](https://forums.swift.org/t/pitch-conditionally-saving-attachments-aka-attachment-lifetimes/82541 "2025-10-06T19:48:50Z")\
**Posts on this page:** 9\
**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:** [October 6, 2025, 7:48pm UTC](https://forums.swift.org/t/pitch-conditionally-saving-attachments-aka-attachment-lifetimes/82541/1 "2025-10-06T19:48:50Z")

</div>

In ST-0009, 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 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 property lets you avoid serializing and saving an attachment if its data won't be necessary (e.g. "if the test passes, don't bother saving this attachment.")

ST-0009 listed attachment lifetimes as a future direction for the feature. Well, _the future is now!_ So I'd like to propose we introduce new API to Swift Testing to allow test authors to control whether or not a test's attachments should be saved or discarded.

Read the full proposal [here](https://github.com/swiftlang/swift-evolution/blob/jgrynspan/conditionally-saving-attachments/proposals/testing/NNNN-conditionally-saving-attachments.md).

## Trying it out

To try this feature out, add a dependency to the `main` branch of `swift-testing` to your project:

```swift
...
dependencies: [
  ...
  .package(url: "https://github.com/swiftlang/swift-testing.git", branch: "main"),
]

```

Then, add a target dependency to your test target:

```swift
.testTarget(
  ...
  dependencies: [
    ...
    .product(name: "Testing", package: "swift-testing"),
  ]
)

```

Finally, import Swift Testing using `@_spi(Experimental) import Testing`.

---

<div class="post-metadata">

**Author:** ![Mordil](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mordil/32/5840_2.png) [@Mordil](https://forums.swift.org/u/Mordil)\
**Post date:** [October 6, 2025, 8:43pm UTC](https://forums.swift.org/t/pitch-conditionally-saving-attachments-aka-attachment-lifetimes/82541/2 "2025-10-06T20:43:24Z")

</div>

> [@grynspan](#):
>
> [here](https://github.com/swiftlang/swift-evolution/blob/jgrynspan/conditionally-saving-attachments/proposals/testing/NNNN-conditionally-saving-attachments.md)

The only piece that I can consider a gap is the ability to say `never` or `always`, to allow overwriting the platform-specific default behavior, and in the cases where you… never or always want the attachments regardless of test outcome

---

<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, 9:01pm UTC](https://forums.swift.org/t/pitch-conditionally-saving-attachments-aka-attachment-lifetimes/82541/3 "2025-10-06T21:01:27Z")

</div>

`always` is the default, however on Apple platforms you have a test plan that may have configured the default differently. @smontgomery had some thoughts about this question he wanted to share.

I am hesitant to include something like `always` in this pitch, at least initially, because it has no real effect on non-Apple platforms (where there's no test plan to override the default.)

---

<div class="post-metadata">

**Author:** ![Mordil](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mordil/32/5840_2.png) [@Mordil](https://forums.swift.org/u/Mordil)\
**Post date:** [October 6, 2025, 9:10pm UTC](https://forums.swift.org/t/pitch-conditionally-saving-attachments-aka-attachment-lifetimes/82541/4 "2025-10-06T21:10:30Z")

</div>

Ah, yes good point.

My only counter-argument is that sometimes developers want to be explicit with their intentionality regardless of the default behaviors of where the test executes, especially when they may have cross-platform support stories for running their test suites (on Apple & Windows for example)

It might be fine as a point for future-direction, just will want to call it out me thinks as such

---

<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:** [October 8, 2025, 10:27pm UTC](https://forums.swift.org/t/pitch-conditionally-saving-attachments-aka-attachment-lifetimes/82541/5 "2025-10-08T22:27:25Z")

</div>

Firstly, thanks @grynspan for working on this. +1 on the overall concept. Having the ability to control when attachments are or are not preserved is important and useful, and I agree with using a trait to control it.

I think this proposal would benefit from two more things:

1. A mechanism to configure the default retention policy: either "_Keep always_" (the current behavior) or "_Discard on success_" (new). This could be specified via a `swift test` CLI argument, environment variable, or some combination thereof.
2. An additional factory function on the proposed `AttachmentSavingTrait` type indicating "keep always". This would primarily be of use when the default (from 1) has been set to "Discard on success", by allowing an individual test or suite to override that default policy.

One big reason I think this is important is to avoid excessive storage usage in CI. If a codebase has many tests which record attachments that are meant to help troubleshoot problems, but those tests are passing, the attachments will often be superfluous and waste both space and potentially incur greater transfer cost for system maintainers. But certain attachments are important regardless of the outcome of a test, so there needs to be a way to reliably preserve those regardless of the default retention policy.

---

<div class="post-metadata">

**Author:** ![maartene](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/maartene/32/42076_2.png) [@maartene](https://forums.swift.org/u/maartene)\
**Post date:** [October 9, 2025, 6:17am UTC](https://forums.swift.org/t/pitch-conditionally-saving-attachments-aka-attachment-lifetimes/82541/6 "2025-10-09T06:17:51Z")

</div>

> [@smontgomery](#):
>
> One big reason I think this is important is to avoid excessive storage usage in CI. If a codebase has many tests which record attachments that are meant to help troubleshoot problems, but those tests are passing, the attachments will often be superfluous and waste both space and potentially incur greater transfer cost for system maintainers.

☝ this.

> [@Mordil](#):
>
> The only piece that I can consider a gap is the ability to say `never` or `always`, to allow overwriting the platform-specific default behavior, and in the cases where you… never or always want the attachments regardless of test outcome

I agree that having a way of overriding environment/test plan/suite defaults is useful. Especially if you run tests in various IDEs and platforms. Having the option to explicitly state in the code how you want to handle saving/retaining attachments makes it more predictable and that to me sounds like a good idea.

---

<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 9, 2025, 12:35pm UTC](https://forums.swift.org/t/pitch-conditionally-saving-attachments-aka-attachment-lifetimes/82541/7 "2025-10-09T12:35:18Z")

</div>

> [@smontgomery](#):
>
> A mechanism to configure the default retention policy: either "_Keep always_" (the current behavior) or "_Discard on success_" (new). This could be specified via a `swift test` CLI argument, environment variable, or some combination thereof.

> [@maartene](#):
>
> I agree that having a way of overriding environment/test plan/suite defaults is useful.

@smontgomery and I have discussed this a bit off-forum and an implementation of this feature would need to include changes to test hosting tools like Swift Package Manager and the VS Code plugin. I do see the value in it!

I think it would make sense to talk to the SwiftPM/VSCP stakeholders about adding a command-line option for this functionality (`--default-attachments-behavior discard` or something like that, API naming is hard) and then proceed from there in a **follow-up proposal**.

Pinging @plemarquand for his thoughts on changes to those components.

---

<div class="post-metadata">

**Author:** ![plemarquand](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/plemarquand/32/32097_2.png) [@plemarquand](https://forums.swift.org/u/plemarquand)\
**Post date:** [October 9, 2025, 3:49pm UTC](https://forums.swift.org/t/pitch-conditionally-saving-attachments-aka-attachment-lifetimes/82541/8 "2025-10-09T15:49:37Z")

</div>

I’m in favour of having a flag for `swift test` to set the default attachment lifetime. Attachment location is already controlled by a flag, so it makes sense for attachment behaviour to be controlled the same way. It’s also easy to integrate in to tools, as VS Code already has the concept of [additionalTestArguments](https://github.com/swiftlang/vscode-swift/blob/main/package.json#L510) that can be forwarded to each invocation, or you can configure the flag on your VS Code launch config.

It does feel like it could be outlined in a follow up proposal though, as its not integral to landing attachment lifetimes as a concept.

---

<div class="post-metadata">

**Author:** ![briancroom](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/briancroom/32/22765_2.png) [@briancroom](https://forums.swift.org/u/briancroom)\
**Post date:** [January 12, 2026, 9:13pm UTC](https://forums.swift.org/t/pitch-conditionally-saving-attachments-aka-attachment-lifetimes/82541/9 "2026-01-12T21:13:04Z")

</div>

After going dormant for a while, this pitch has moved into formal review, available [here](https://forums.swift.org/t/st-0018-conditionally-saving-attachments/84051)!
