swift-testing
Use when writing or reviewing tests in Swift with the Swift Testing framework, including tests for concurrent or thread-shared state.
- License 许可
- MIT
- 最近更新
- 2026-11-13
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- swift-testing
- description
- Use when writing or reviewing tests in Swift with the Swift Testing framework, including tests for concurrent or thread-shared state.
02 资源文件 1
- 参考文档references/concurrency.md9.0 KB
03 SKILL.md 正文
Swift Testing
REQUIRED BACKGROUND: vvkit:writing-tests — when a test is owed, unit vs component, and the
acceptance filter. This covers only the framework: @Test, @Suite, #expect, #require,
confirmation.
Not XCTest. UI automation (XCUITest) is XCTest, and a suite already in XCTest stays there — two frameworks in one suite split setup, assertions and failure output.
#expect can silently assert nothing
#expect((model?.ads ?? []).isEmpty) // asserts NOTHING
-
This compiles, passes and checks nothing: the macro rewrites it to a closure over a non-optional receiver and discards the result. The only signal is a warning (
expression of type 'Bool?' is unused); related expressions are hard errors, so the compiler is no guard. -
Never feed
#expectoptional chaining mixed with a trailing closure or??. Hoist it:let ads = model?.ads ?? [] #expect(ads.isEmpty) -
Treat any macro-expansion warning as a correctness failure. A test asserting nothing reports as coverage — worse than a missing test.
Shape of a test
import Testing
@testable import ExampleFramework
@Suite("Mapper")
struct MapperTests {
@Test("maps a paused item to the 2:1 shape")
func mapsPausedShape() throws {
let mapped = try #require(Mapper.map(.fixture()))
#expect(mapped.shape == .twoToOne)
}
@Test("rejects every unsupported shape", arguments: [Shape.bar, .full])
func rejectsUnsupported(shape: Shape) {
#expect(Mapper.map(.fixture(shape: shape)) == nil)
}
}
@Suiteon astruct— a fresh instance per test.final classwithdeinitonly for real teardown — temp files, database pools.- Setup in
init(). There is nosetUp/tearDown. == true/== falseover a bare boolean —#expect(!x)prints nothing useful on failure.try #require(x), not a non-nil assertion then a force-unwrap.Equatablestate and result types, so one#expect(state == expected)checks every field.@Test(arguments:)only for table-driven cases — same body, different data.- Input/expected pairs as one collection of case structs.
arguments: inputs, expectedruns the cartesian product;zipsilently drops an unpaired input. Cases run in parallel —.serializedif they share state. - Human-sentence display names in
@Test("…").
Making an async assertion land
In this order; move down only when the tool above cannot express it.
- Virtual time. Inject a scheduler or clock the test controls. No seam: propose one — a
design finding. For async/await — task groups, timeouts, cancellation — the seam is a spy the test
releases:
vvkit-swift:swift-testing-async. confirmationfor "fires exactly N times" and "stays silent" (expectedCount: 0). It fails with "confirmed 0 times, but expected 1", not a bare timeout.- Polling, last resort, for wall-clock work. Poll to synchronise, then assert in a separate
#expectso the value prints. For non-nil, poll the predicate thentry #require. Scale deadlines by an environment knob, not edited timeouts.
Running a subset: the logged name is not the identifier
@Suite("…") / @Test("…") set a display name — what the log prints. -only-testing takes the
Swift identifier: the type for a suite, the function with parentheses for a test.
@Suite("Currency") // display name — appears in the log
struct CurrencyTests { // ← this is what -only-testing needs
@Test("currency codes compare case-insensitively")
func codeComparisonIgnoresCase() { ... } // ← and this
}
| Scope | Identifier |
|---|---|
| Whole bundle | AppTests |
| One suite | AppTests/CurrencyTests |
| One test | AppTests/CurrencyTests/codeComparisonIgnoresCase() |
- A display name copied from the log matches nothing, and
xcodebuildruns zero tests and exits 0. Read the count; a test script that does not fail on zero reports success for an empty filter. - Nested suites follow the type path, not display names. When in doubt, take the identifier from source.
Coverage
xcodebuild test -enableCodeCoverage YES …, then xcrun xccov view --report --json <bundle>.xcresult
for per-function numbers. Without the flag the report is empty and nothing warns.
Watchdogs
.timeLimitcannot catch a cooperative-pool deadlock. It is minutes-only, a task group racing a sleep: a task blocked on a lock never cancels, and the timeout task needs the same wedged pool.- A working watchdog needs a dedicated non-cooperative thread. Use the project's; testing lock-based code without one, add it once.
Concurrency
Code crossing a thread, queue or isolation boundary — locks, dispatch queues, timers, KVO, delegate
callbacks, actors — is a different problem from landing an async assertion. Shapes, the weakest-tool
ladder and the first-access trap: references/concurrency.md.
04 同仓库更多 Skills
no-heading
Use when proving that the validator rejects a body with no H1 and a force-loading @-link.
wrong-name-does-not-match-dir
Dispatches a subagent per task and reviews between each one.
writing-tests
Use when adding or changing behavior, fixing a bug, or judging whether an existing or generated test earns its place.
writing-skills
Use when authoring or editing a skill, agent, command or hook for this kit, or when deciding whether a piece of guidance belongs in a skill at all.
writing-project-instructions
Use when creating or editing a CLAUDE.md or AGENTS.md, when it has grown long, or when a rule in it also exists in an installed skill.
writing-logs
Use when adding or reviewing log statements, or when deciding what level a message belongs at.