swift-testing
Skill4 starsChanged 9 months agoMITView on GitHub
Writes unit tests and UI tests for Swift, SwiftUI, and UIKit projects following enterprise or indie testing standards. Use when user says "write tests", "add unit tests", "test this code", "create test cases", "add XCTest", "write Swift Testing tests", "help me test this", or "add tests for". Applies enterprise or indie standard from references/ based on project CLAUDE.md config.
8 other projects use a version of this.
Adopted by
Most recently taken up by
- Fe2-O3/swift-field-guidetheir own version, first seen Sep 9, 2026
- fiveonecode/agent-skillstheir own version, first seen Sep 6, 2026
- ozgurakkaya90/claude-skillstheir own version, first seen Aug 2, 2026
- dpearson2699/swift-ios-skillstheir own version, first seen Jul 13, 2026
- Viniciuscarvalho/swift-code-reviewer-skilltheir own version, first seen Apr 26, 2026
- Prisma-Labs-Dev/apple-skillstheir own version, first seen Apr 14, 2026
Other versions8 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 8
- dpearson2699/swift-ios-skills★ 1.2ksame name
- Prisma-Labs-Dev/apple-skills★ 337same name
- bocato/swift-testing-agent-skill★ 80same name
- Viniciuscarvalho/swift-code-reviewer-skill★ 37same name
- fiveonecode/agent-skills★ 19same name
- pluginagentmarketplace/custom-plugin-swift★ 9same name
- Fe2-O3/swift-field-guide★ 0same name
- ozgurakkaya90/claude-skills★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository7 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- swift-architecture-auditSkill
- swift-code-reviewSkill
- swift-networkingSkill
- swift-persistenceSkill
- swift-project-setupSkill
- swiftui-componentSkill
The file
We split this skill into the rules it gives an agent
10 rules. 0 are common, 6 are distinctive, and 4 are very specific to this project.
0
Common
Most of the 7 edited versions we read keep these.
6
Distinctive
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 16
Check CLAUDE.md for Testing Mode:
- Distinctive: line 19
If not set: ask the user which mode applies
- Distinctive: line 23
Ask if not already clear:
- Distinctive: line 50
Do NOT mix Swift Testing and XCTest in the same test target.
- Distinctive: line 120
Always generate a mock alongside any test that needs one:
- Distinctive: line 145
Before finalizing, verify every test:
- Very specific: line 160
Read ProductListViewModel.swift to understand state and dependencies
- Distinctive: line 163
Write tests: empty state, loaded state, error state, loading indicator
- Very specific: line 171
Write Swift Testing @Test functions for: zero cents, typical value, large value, locale edge cases
- Very specific: line 181
"Cannot find type X in scope": Add @testable import YourModule at the top of the test file.
“Common” is measured: kept by at least 6 in 10 of the 7 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Swift Testing
Instructions
Step 1: Determine Testing Mode
Check CLAUDE.md for Testing Mode:
enterprise→ consultreferences/enterprise.mdfor strict rulesindie→ consultreferences/indie.mdfor pragmatic rules- If not set: ask the user which mode applies
Step 2: Identify What to Test
Ask if not already clear:
- Which type / function / feature needs tests?
- What is the expected behavior (not implementation)?
- Are there async operations?
- Are there existing mocks or does the skill need to create them?
Always test:
- ViewModels (state changes, actions, error states)
- Services and Repositories (business logic, data transformations)
- Formatters, validators, utilities
- Async flows end-to-end (with mocked dependencies)
- Edge cases and error paths
Never test:
- Private implementation details
- SwiftUI view tree structure or layout positions
- UIViewController internal wiring
Step 3: Choose the Framework
| Scenario | Framework |
| New unit tests (Swift 5.9+, iOS 16+) | Swift Testing (@Test, #expect) |
| Legacy or team convention on XCTest | XCTest (XCTestCase, XCTAssert*) |
| UI / end-to-end tests | XCTest + XCUITest |
| Async unit tests | Both support async — use await |
Do NOT mix Swift Testing and XCTest in the same test target.
Step 4: Write Tests
Naming (both frameworks):
test_when_<Condition>_should_<ExpectedOutcome>()Structure — Swift Testing:
import Testing
@testable import MyApp
@MainActor
struct LoginViewModelTests {
@Test func test_when_credentialsAreValid_should_setIsLoggedIn() async throws {
// Arrange
let authService = MockAuthService(willSucceed: true)
let sut = LoginViewModel(authService: authService)
// Act
await sut.login(email: "user@example.com", password: "secret")
// Assert
#expect(sut.isLoggedIn == true)
#expect(sut.error == nil)
}
@Test func test_when_credentialsAreInvalid_should_setError() async throws {
// Arrange
let authService = MockAuthService(willSucceed: false)
let sut = LoginViewModel(authService: authService)
// Act
await sut.login(email: "bad@example.com", password: "wrong")
// Assert
#expect(sut.isLoggedIn == false)
#expect(sut.error != nil)
}
}Structure — XCTest:
import XCTest
@testable import MyApp
@MainActor
final class LoginViewModelTests: XCTestCase {
func test_when_credentialsAreValid_should_setIsLoggedIn() async throws {
// Arrange
let authService = MockAuthService(willSucceed: true)
let sut = LoginViewModel(authService: authService)
// Act
await sut.login(email: "user@example.com", password: "secret")
// Assert
XCTAssertTrue(sut.isLoggedIn)
XCTAssertNil(sut.error)
}
}Step 5: Generate Mocks
Always generate a mock alongside any test that needs one:
// Always inject via protocol, never use real implementations in unit tests
final class MockAuthService: AuthServiceProtocol {
let willSucceed: Bool
private(set) var loginCallCount = 0
init(willSucceed: Bool) {
self.willSucceed = willSucceed
}
func login(email: String, password: String) async throws -> User {
loginCallCount += 1
if willSucceed {
return User(id: "1", email: email)
} else {
throw AuthError.invalidCredentials
}
}
}Step 6: Determinism Checklist
Before finalizing, verify every test:
- No real network calls
- No
sleep()or polling - No shared global state between tests
- No real time/date dependency (inject
Dateor a clock protocol) - Fresh
sutinstance per test setUp/tearDownonly when truly necessary
Examples
Example 1: ViewModel with async fetch
User says: "Write tests for my ProductListViewModel that fetches products from an API"
Actions:
- Read
ProductListViewModel.swiftto understand state and dependencies - Identify the
ProductRepositoryProtocolor create it if missing - Generate
MockProductRepositorywith stubbable results - Write tests: empty state, loaded state, error state, loading indicator
Example 2: Data transformation
User says: "Test my PriceFormatter that converts cents to currency string"
Actions:
- Read
PriceFormatter.swift - Write Swift Testing
@Testfunctions for: zero cents, typical value, large value, locale edge cases
Troubleshooting
Async tests fail intermittently: You are using sleep() or polling. Replace with async/await and mocked async dependencies that resolve immediately.
Tests break on refactoring: You are testing implementation details (private properties, internal call counts). Refocus on observable output state only.
ViewModel is untestable: Dependencies are created inside the type (NetworkService()). Inject them via init. This is an architecture issue — see swift-architecture-audit skill.
"Cannot find type X in scope": Add @testable import YourModule at the top of the test file.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

