# \[Pitch\] Colocated Tests

**URL:** <https://forums.swift.org/t/pitch-colocated-tests/91282>\
**Category:** Pitches\
**Tags:** packagemanager, swift-testing\
**Created:** [December 1, 2026, 6:14pm UTC](https://forums.swift.org/t/pitch-colocated-tests/91282 "2026-12-01T18:14:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jerryjrchen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jerryjrchen/32/42483_2.png) [@jerryjrchen](https://forums.swift.org/u/jerryjrchen)\
**Post date:** [December 1, 2026, 6:14pm UTC](https://forums.swift.org/t/pitch-colocated-tests/91282/1 "2026-12-01T18:14:16Z")

</div>

Hello Swift Testing enthusiasts!

Since time immemorial, packages have required a separate test target. This adds an extra step whenever you want to add new tests or find relevant tests. A separate target also can't access private declarations for testing purposes.

To address these limitations, I've written a proposal about "colocated tests", where library targets can place tests alongside implementation code.

## In Practice

Opt-in to colocated tests in `Package.swift`:

```swift
// Package.swift

let package = Package(
  name: "ColocatedTestExample",
  ...
  targets: [
    // .includesTests means the library target contains colocated tests.
    // This is referred to as a "colocated test target"
    .target(name: "HousingLibrary", testOptions: .includesTests),
  ],
  ...
)

```

`import Testing` and start writing tests!

```swift
// Duplex.swift
// HousingLibrary

import Testing

struct Duplex {
  fileprivate var address: String
  private var residents: Int

  init(address: String = "123 Any Street", residents: Int = 2) {
    self.address = address
    self.residents = residents
  }
}

// MARK: Colocated Tests
// Everything below here is excluded when building for distribution.

// Custom Availability Domain: marks this test helper as test-only content
@available(Testing)
func validate(address: String) {
  if !address.starts(with: /[0-9]+/) {
    Issue.record("Missing house number: \(address)")
  }
}

extension Duplex {
  @Test func `Check private field resident count in an extension`() {
    #expect(Duplex(residents: 4).residents == 4)
  }
}

struct DuplexTests {
  @Test func `Check fileprivate field address in separate suite`() {
    validate(address: Duplex(address: "456 Other Street").address)
  }
}

```

## How it works

Only library targets that opt-in to colocated tests (a “colocated test target”) are affected by this proposal. Here are the high-level changes:

- Declare a testing custom availability domain `@available(Testing)` which annotates test-only content. Testing library symbols will be annotated with it, and macros like `@Test` will automatically apply it. You’ll need to manually annotate your own test helpers and other test-only symbols.
- SwiftPM builds and runs tests in colocated test targets when building with `--configuration debug`.
- To prevent distributing test code, SwiftPM disables the Testing availability domain when building with `--configuration release` (unless opting in with `--build-tests`).

## Discussion

I'd be really curious to hear if colocated tests would improve your testing experience!

Consider also:

- Could you meet all your testing needs with colocated tests? What use cases would still depend on regular test targets?

- Availability domain naming: `@available(Testing)` vs `@available(Development)` vs something else.

Please check out the full proposal: [swift-evolution/proposals/testing/nnnn-colocated-tests.md at colocated-tests · jerryjrchen/swift-evolution · GitHub](https://github.com/jerryjrchen/swift-evolution/blob/colocated-tests/proposals/testing/nnnn-colocated-tests.md)

---

<div class="post-metadata">

**Author:** ![AlexisQapa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexisqapa/32/7159_2.png) [@AlexisQapa](https://forums.swift.org/u/AlexisQapa)\
**Post date:** [December 1, 2026, 6:34pm UTC](https://forums.swift.org/t/pitch-colocated-tests/91282/2 "2026-12-01T18:34:11Z")

</div>

As someone who almost always create one test target per target and then spend my day switching schemes this is great. My first reaction/concern was keeping release testing and shipping test/help code and I like how you handled that with good defaults.

One thing I’d like you to detail is how would one declare a test helper that is both importable in multiples modules and also only built while testing ? Can we simply use the same availability domain ? Do we need the helper module to be declared as a colocated module to get access to the domain ?

---

<div class="post-metadata">

**Author:** ![jerryjrchen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jerryjrchen/32/42483_2.png) [@jerryjrchen](https://forums.swift.org/u/jerryjrchen)\
**Post date:** [December 1, 2026, 8:48pm UTC](https://forums.swift.org/t/pitch-colocated-tests/91282/3 "2026-12-01T20:48:03Z")

</div>

Thanks for the feedback!

> [@AlexisQapa](#):
>
> One thing I’d like you to detail is how would one declare a test helper that is both importable in multiples modules and also only built while testing ? Can we simply use the same availability domain ? Do we need the helper module to be declared as a colocated module to get access to the domain ?

If you have a library that's a collection of test helpers, then you'd declare it as a test helper library for proper handling (`.target(testOptions: .testHelper)`). You would apply the same testing availability domain to the helpers, and `import Testing` would be enough to get access to the domain. The proposal explains this in depth: [swift-evolution/proposals/testing/nnnn-colocated-tests.md at colocated-tests · jerryjrchen/swift-evolution · GitHub](https://github.com/jerryjrchen/swift-evolution/blob/colocated-tests/proposals/testing/nnnn-colocated-tests.md#test-helper-libraries)

---

<div class="post-metadata">

**Author:** ![benlings](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benlings/32/13011_2.png) [@benlings](https://forums.swift.org/u/benlings)\
**Post date:** [December 1, 2026, 9:04pm UTC](https://forums.swift.org/t/pitch-colocated-tests/91282/4 "2026-12-01T21:04:09Z")

</div>

> [@jerryjrchen](#):
>
> I'd be really curious to hear if colocated tests would improve your testing experience!

This is one of my favourite parts about writing Go code - the tests are right next to the code being tested. It not only makes it easier to find the relevant test for some code, it also makes it far more obvious that it’s _not_ there. Having it the same file (SwiftUI-Preview-like) sounds like a big improvement.
