.agents/skills/swift-testing-expert/references/fundamentals.md
Use this file when creating new Swift Testing suites or refactoring test structure before deeper topics like traits, parameterization, or migration.
Testing only in test targets.@Test to declare tests explicitly (global function or type method).struct, actor, or class) to group related tests.struct suites for value semantics and accidental-state-sharing prevention.@Suite when adding suite-level traits or display names.import Testing
@testable import FoodTruck
@Test("Food truck has a valid default name")
func defaultName() {
let truck = FoodTruck()
#expect(truck.name.isEmpty == false)
}
import Testing
@testable import FoodTruck
@Suite("Menu tests")
struct MenuTests {
@Test("Returns no duplicates")
func uniqueItems() {
let items = Menu.default.items
#expect(Set(items).count == items.count)
}
}
import Testing
@testable import FoodTruck
struct CheckoutTests {
struct Taxes {
@Test func taxIsRoundedToTwoDigits() {
let total = Checkout.total(subtotal: 10.00, taxRate: 0.0825)
#expect(total == 10.83)
}
}
struct Discounts {
@Test func promoCodeAppliesFixedAmount() {
let total = Checkout.total(subtotal: 20.00, discount: .fixed(5))
#expect(total == 15.00)
}
}
}
test... prefixes.@MainActor only when code under test requires main-thread isolation.@available on test functions when needed for platform/language gating.@available to suite declarations.import Testing
@Suite
struct SessionTests {
let config: URLSessionConfiguration
// Valid: callable with zero args due to default value.
init(config: URLSessionConfiguration = .ephemeral) {
self.config = config
}
@Test func usesEphemeralByDefault() {
#expect(config == .ephemeral)
}
}
import Testing
// Do not do this on suite types:
// @available(iOS 18, *)
@Suite
struct PushTests {
@available(iOS 18, *)
@Test func supportsNewPushFormat() {
#expect(true)
}
}
@available; annotate test functions instead.Testing, app targets do not.struct/actor/class) matches setup and teardown needs.