swift_testing

Swift Testing framework for unit tests, integration tests, and async testing with modern syntax.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "swift_testing" with this command: npx skills add swiftzilla/skills/swiftzilla-skills-swift-testing

Swift Testing

This skill covers the modern Swift Testing framework introduced in Swift 5.9 for writing clean, expressive tests.

Overview

Swift Testing is Apple's modern testing framework that provides a more expressive and flexible way to write tests compared to XCTest. It features Swift-native syntax, parameterized tests, and better async support.

Available References

Quick Reference

Basic Test

import Testing

@Test
func addition() {
    let result = 1 + 1
    #expect(result == 2)
}

@Test
func throwsError() throws {
    #expect(throws: MyError.invalid) {
        try riskyOperation()
    }
}

Async Test

@Test
func fetchData() async throws {
    let data = try await fetchUser()
    #expect(data.name == "Ada")
}

Parameterized Test

@Test(arguments: ["hello", "world", "swift"])
func stringLength(string: String) {
    #expect(string.count > 0)
}

@Test(arguments: zip([1, 2, 3], [1, 4, 9]))
func squared(input: Int, expected: Int) {
    #expect(input * input == expected)
}

Test Suite

@Suite
struct CalculatorTests {
    let calculator = Calculator()
    
    @Test
    func add() {
        #expect(calculator.add(2, 3) == 5)
    }
    
    @Test
    func subtract() {
        #expect(calculator.subtract(5, 3) == 2)
    }
}

Swift Testing vs XCTest

FeatureSwift TestingXCTest
SyntaxNative SwiftObjective-C heritage
AsyncFirst-classAdded later
ParametersBuilt-inManual loops
Assertions#expect, #requireXCTAssert
Suite@SuiteClass-based

Best Practices

  1. Use descriptive names - Test names should explain behavior
  2. Test one thing - Each test should verify one concept
  3. Use #require for preconditions - Fail fast if setup fails
  4. Leverage parameterized tests - Test multiple inputs efficiently
  5. Organize with suites - Group related tests
  6. Use tags - Categorize tests for selective running
  7. Test async code - Use async test functions
  8. Keep tests independent - No shared state between tests

Running Tests

# Run all tests
swift test

# Run specific test
swift test --filter addition

# Run tests by tag
swift test --tag unit

# Run with verbose output
swift test --verbose

For More Information

Visit https://swiftzilla.dev for comprehensive Swift Testing documentation.

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

swift_swiftui

No summary provided by upstream source.

Repository SourceNeeds Review
General

swift_concurrency

No summary provided by upstream source.

Repository SourceNeeds Review
General

swift_style

No summary provided by upstream source.

Repository SourceNeeds Review
General

swift_package_manager

No summary provided by upstream source.

Repository SourceNeeds Review