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:
// 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!
// 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@Testwill 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?
One example might be development assets, which would still require a separate test target for isolation.
-
Availability domain naming:
@available(Testing)vs@available(Development)vs something else.A more generic "development only" availability domain might be useful. One example is the
#Playgroundmacro (forum pitch), which could use this domain to wrap playground code snippets. It might feel out of place to call a helper annotated with the testing domain within a#Playgroundblock. Conversely, a test helper annotated with the development domain might lack context about its ultimate use case.
Please check out the full proposal: swift-evolution/proposals/testing/nnnn-colocated-tests.md at colocated-tests · jerryjrchen/swift-evolution · GitHub