Swift Wants Your Tests in the Same File as Your Code. The Hard Part Is Keeping Them Out of Your Binary.
Every Swift developer has done the dance. You fix a bug in PriceFormatter.swift, you want to add a test, and now you’re scrolling the project navigator looking for PriceFormatterTests.swift, which lives in a different folder, in a different target, behind a different scheme, under a name somebody chose in 2021.
Then you find out the thing you wanted to test is private. So you make it internal, add @testable import, and feel slightly dirty about it for the rest of the afternoon.
On December 1st, a new Swift Testing pitch proposed ending the dance. It’s called “Colocated Tests”, and the idea is simple enough to fit in a tweet: let a library target hold its own tests, in the same files as the code. The forums mostly reacted the way you’d expect. One reply was just “I’ve wanted this for a decade.” I get it.
The interesting part isn’t putting tests next to code. Any language can do that. The interesting part is how Swift proposes to make sure those tests never ship to your users.
What the pitch actually looks like
You opt a library target in from Package.swift:
// Package.swift
let package = Package(
name: "Pricing",
targets: [
.target(name: "Pricing", testOptions: .includesTests)
]
)
And then you just… write tests. In the source file. Next to the type:
// PriceFormatter.swift (target: Pricing)
import Foundation
import Testing
struct PriceFormatter {
private let currencyCode: String
init(currencyCode: String) {
self.currencyCode = currencyCode
}
func format(cents: Int) -> String {
let amount = Decimal(cents) / 100
return amount.formatted(.currency(code: currencyCode))
}
}
// MARK: - Tests (compiled out of release builds)
extension PriceFormatter {
@Test func `keeps the currency code it was given`() {
#expect(PriceFormatter(currencyCode: "EUR").currencyCode == "EUR")
}
}
Look at that test for a second. It reads currencyCode, which is private. Because the test is an extension in the same file, normal Swift access control already allows it. No @testable, no widening the API, no “internal for testing” comments that everyone ignores.
If you’ve used Rust’s #[cfg(test)] mod tests or Go’s _test.go files, this will feel familiar. Swift has been the odd one out here for a long time.
The real trick: a “Testing” availability domain
Here’s the problem with tests living in your library: your library ships. If the test code compiles into the release binary, you’ve just shipped Issue.record and a link dependency on the Testing framework to the App Store. Nobody wants that.
The pitch solves it with something Swift doesn’t officially have yet: a custom availability domain. You already know availability annotations:
@available(iOS 18, *)
func useTheNewThing() { }
That says “this only exists on iOS 18 and later.” The pitch adds a domain that isn’t a platform version at all. It’s just a switch:
@available(Testing)
func makeSampleFormatter() -> PriceFormatter {
PriceFormatter(currencyCode: "USD")
}
Think of it like a lamp on a smart plug. The lamp (your test helper) is always wired in, but the build system decides whether the plug is on:
- Debug builds: the Testing domain is enabled. SwiftPM builds and runs your colocated tests.
- Release builds: the domain is disabled, so the code is compiled out. You can force tests back in with
--build-tests.
@Test and the other Testing macros would apply the annotation for you. Your own helpers, fixtures and sample data need it manually. That’s the part I’d expect to bite people. Forget the annotation on a helper, and it’s no longer test-only code. It’s just code.
One Swift compiler engineer in the thread was refreshingly upfront that custom availability domains haven’t been through Swift Evolution yet, and that they plan to re-pitch them with this as a motivating use case. So this pitch is standing on a foundation that is itself still a pitch. Keep your excitement at “promising,” not “ship it Monday.”
The questions the thread is already asking
Pitches are where the good critique happens, and this one got some sharp replies in its first 48 hours.
“Do I really have to annotate my test structs?” The proposal’s own example has a struct DuplexTests holding a @Test function, with no @available(Testing) on the struct itself. A commenter pointed out that the struct’s type metadata would presumably still land in release builds even if the test function doesn’t. Small thing, but it’s exactly the kind of leak the whole design exists to prevent.
Build times. Tests in a disabled domain are still type-checked in every configuration. That’s on purpose. According to the compiler engineer’s reply, code in a disabled domain is treated as statically unreachable, so it never gets lowered to SIL, and the expensive optimization and code generation phases skip it. Type checking still happens, though. If your test suite is three times the size of your production code (be honest, or be aspirational), that isn’t free.
Incremental builds. A SwiftPM maintainer raised something I hadn’t thought about. Lots of people build without tests while iterating, then flip to --build-tests when they’re ready. If tests live inside the target, toggling them potentially rebuilds the target itself, not some separate test module nobody depends on. They floated a test configuration that’s separate from debug vs. release, which honestly sounds right to me.
Linking. Brandon Williams brought up a nasty one that exists today. If a helper library uses Testing symbols and you link it into a command-line tool, the tool can crash at launch with Library not loaded: @rpath/Testing.framework, even if you never call the testing code. Colocated tests make “library that touches Testing” far more common, so that problem has to be solved, not just inherited.
None of these kill the idea. They’re the questions that separate a good proposal from a merged one.
So what does this mean for your iOS app?
Here’s the honest part. The pitch is about SwiftPM library targets. It doesn’t say anything about Xcode app targets or schemes, so I wouldn’t assume your .xcodeproj gets this on day one.
But look at how most serious iOS projects are structured in 2026. The app target is a thin shell, and the real code lives in local Swift packages: FeatureCheckout, Networking, DesignSystem. That’s the structure we push in the course and in the Swift Testing migration guide. If your code already lives in packages, colocated tests land exactly where your logic is.
A few practical takes:
- Start modularizing now if you haven’t. Every feature you move into a package becomes a candidate for colocated tests later. And it gets you faster builds and cleaner boundaries today, so it’s not a speculative bet.
- Don’t rush to test private members. One reply in the thread said it well: needing to test something
privateis often feedback about your design, not a reason to reach in. Colocation makes it possible, not recommended. If your view model is hard to test from its public surface, the red-green-refactor workflow for @Observable view models is a better fix. - Keep integration tests separate. Even the enthusiastic replies estimated that colocated tests cover maybe 90% of unit tests, not everything. Anything that talks to the network, a database, or the file system belongs in its own target, where you can see it and run it on purpose.
- Watch how this pairs with SwiftPM traits. I wrote about the traits-for-test-targets pitch back in September. Tests living inside a trait-configured target raise fun new questions about which configuration your tests even see.
The quiet AI angle
There’s one more reason I like this, and it has nothing to do with Swift purity.
Coding agents are bad at finding tests. When an agent edits PriceFormatter.swift, it has to guess where the matching tests live, and plenty of times it just doesn’t look. A study on agents and testing I covered in September found that simply telling an agent to “use TDD” barely moves the needle.
A test sitting thirty lines below the function it covers is very hard to miss. For humans, sure. But especially for a model that reads the file it’s editing and not much else. If you’re using AI to chase down bugs, the habits in our debugging with AI lesson get noticeably easier when the test is literally in the context window.
As one commenter put it about Go, having the test right there also makes it painfully obvious when it isn’t there. An empty // MARK: - Tests section is a much louder signal than a missing file in some other folder.
Where it stands
This is a pitch, posted December 1, 2026. Not an accepted proposal, not in a toolchain, and it depends on custom availability domains, which are themselves heading back to the pitch stage. Realistically, you’re looking at months, not weeks.
But the shape is right. Tests next to code, compiled out by default, access control that just works. Swift Testing already made tests nicer to write. This would make them nicer to find, and those are the ones that actually get written.
If you’ve got opinions, especially about how this should work for Xcode app targets, the forum thread is open. It’s early enough that your feedback could actually change the design.
Share this post
Comments
Leave a comment
NativeFirst Team
EditorialThe NativeFirst team — engineers and designers building native Apple apps and writing the courses we wish we had when we started.