# \[Pitch\] Add a \`.taskLocal\` trait to Swift Testing

**URL:** <https://forums.swift.org/t/pitch-add-a-tasklocal-trait-to-swift-testing/87603>\
**Category:** Pitches\
**Tags:** swift-testing\
**Created:** [June 22, 2026, 6:42pm UTC](https://forums.swift.org/t/pitch-add-a-tasklocal-trait-to-swift-testing/87603 "2026-06-22T18:42:47Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![mbrandonw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mbrandonw/32/349_2.png) [@mbrandonw](https://forums.swift.org/u/mbrandonw)\
**Post date:** [June 23, 2026, 3:32pm UTC](https://forums.swift.org/t/pitch-add-a-tasklocal-trait-to-swift-testing/87603/5 "2026-06-23T15:32:56Z")

</div>

Yeah, technically Swift Testing _today_ is capable of doing everything you want thanks to `TestScoping`, but this proposal can make things a little nicer.

And `ServiceContext` is a good example of how this proposal can do that. Right now [swift-service-context](https://github.com/apple/swift-service-context) vends a task local that others will want to bind in tests. Currently there are two ways to do that:

- Anyone that uses `ServiceContext` defines their own test trait for binding the task local in their tests.
- Or, swift-service-context can create a whole new ServiceContextModuleTestSupport library that creates this trait a single time, and then anyone that wants to bind the task local in their tests will depend on this library.

Neither option is great. The first leads to a lot of duplication across different projects, and the second is extra code and documentation to maintain for the library author (also test support library docs [can't link](https://forums.swift.org/t/a-preview-of-doccs-support-for-combined-documentation/74373/4) to the main library in DocC), and they have to educate their users about how to use the test support library. And _every_ library that has a `@TaskLocal` will need to do this, and the docs for these traits are basically all identical.

So, if this proposal is accepted, anybody using swift-service-context can just do this:

```swift
@Suite(.taskLocal(ServiceContext.$current, …))
struct MySuite {
  …
}

```

And the same goes for any task locals DistributedTracing introduces.

---

_[View the full topic](https://forums.swift.org/t/pitch-add-a-tasklocal-trait-to-swift-testing/87603)._
