Library Moduleswift-testing 6.0.1Testing
Testing
Create and run tests for your Swift packages and Xcode projects.
Documentation.mdimport Testing
Module information
- Declarations
- 519
- Symbols
- 615
Overview
With Swift Testing you leverage powerful and expressive capabilities of the Swift programming language to develop tests with more confidence and less code. The library integrates seamlessly with Swift Package Manager testing workflow, supports flexible test organization, customizable metadata, and scalable test execution.
Define test functions almost anywhere with a single attribute.
Group related tests into hierarchies using Swift’s type system.
Integrate seamlessly with Swift concurrency.
Parameterize test functions across wide ranges of inputs.
Enable tests dynamically depending on runtime conditions.
Parallelize tests in-process.
Categorize tests using tags.
Associate bugs directly with the tests that verify their fixes or reproduce their problems.
Related videos
<Links>
- visualStyle
- compactGrid
Essentials
Getting started
Start running tests in a new or existing XCTest-based test target.
Read MoreDefining test functions
Define a test function to validate that code is working correctly.
Read MoreOrganizing test functions with suite types
Organize tests into test suites.
Read MoreMigrating a test from XCTest
Migrate an existing test method or test class written using XCTest.
Read Moremacro Test(String?, any TestTrait...
) Declare a test.
struct Test
A type representing a test or suite.
macro Suite(String?, any SuiteTrait...
) Declare a test suite.
Test parameterization
Implementing parameterized tests
Specify different input parameters to generate multiple test cases from a test function.
Read Moremacro Test<C>(String?, any TestTrait..., arguments: C
) Declare a test parameterized over a collection of values.
macro Test<C1, C2>(String?, any TestTrait..., arguments: C1, C2
) Declare a test parameterized over two collections of values.
macro Test<C1, C2>(String?, any TestTrait..., arguments: Zip2Sequence<C1, C2>
) Declare a test parameterized over two zipped collections of values.
protocol CustomTestArgumentEncodable
A protocol for customizing how arguments passed to parameterized tests are encoded, which is used to match against when running specific arguments.
struct Case
A single test case from a parameterized
Test
.
Behavior validation
Expectations and confirmations
Check for expected values, outcomes, and asynchronous events in tests.
Read MoreKnown issues
Highlight known issues when running tests.
Read More
Test customization
Uncategorized
Protocols
protocol CustomTestStringConvertible
A protocol describing types with a custom string representation when presented as part of a test’s output.
Show implementation details (1)
Hide implementation details
protocol __TestContainer
A protocol describing a type that contains tests.
Show system interfaces (3)
Hide system interfaces
protocol CustomExecutionTrait
A protocol extending
Trait
that offers an additional customization point for trait authors to execute code before and after each test function (if added to the traits of a test function), or before and after each test suite (if added to the traits of a test suite).protocol SPIAwareTrait
A protocol describing traits that can be added to a test function or to a test suite and that make use of SPI symbols in the testing library.
protocol __ExitTestContainer
A protocol describing a type that contains an exit test.
Types
struct Issue
A type describing a failure or warning which occurred during a test.
struct SourceLocation
A type representing a location in source code.
struct Tag
A type representing a tag that can be applied to a test.
Show implementation details (2)
Hide implementation details
struct __CommandLineArguments_v0
A type describing the command-line arguments passed by Swift Package Manager to the testing library’s entry point.
struct __Expression
A type representing a Swift expression captured at compile-time from source code.
Show system interfaces (10)
Hide system interfaces
enum ABIv0
A namespace for ABI version 0 symbols.
struct Backtrace
A type representing a backtrace or stack trace.
struct Configuration
A type containing settings for preparing and running tests.
struct Event
An event that occurred during testing.
enum ExitCondition
An enumeration describing possible conditions under which an exit test will succeed or fail.
struct ExitTest
A type describing an exit test.
struct Runner
A type that runs tests according to a given configuration.
struct SkipInfo
A type representing the details of a skipped test.
struct SourceContext
A type representing the call stack backtrace and source location of a particular call.
struct TypeInfo
A description of the type of a value encountered during testing or a parameter of a test function.
Typealiases
Show implementation details (1)
Hide implementation details
Show system interfaces (1)
Hide system interfaces
typealias Expression
A type representing a Swift expression captured at compile-time from source code.
Macros
macro Tag(
) Declare a tag that can be applied to a test function or test suite.
Show implementation details (1)
Hide implementation details
macro _sourceLocation(
) -> SourceLocation Get the current source location as a compile-time constant.
Show system interfaces (2)
Hide system interfaces
macro expect(exitsWith: ExitCondition, @autoclosure () -> Comment?, sourceLocation: SourceLocation, performing: @convention(thin) () async throws -> Void
) Check that an expression causes the process to terminate in a given fashion.
macro require(exitsWith: ExitCondition, @autoclosure () -> Comment?, sourceLocation: SourceLocation, performing: @convention(thin) () async -> Void
) Check that an expression causes the process to terminate in a given fashion and throw an error if it did not.
Globals
func confirmation<R>(Comment?, expectedCount: Int, sourceLocation: SourceLocation, (Confirmation) async throws -> R
) async rethrows -> R Confirm that some event occurs during the invocation of a function.
Show implementation details (38)
Hide implementation details
var __requiringAwait: Void
A value that abstracts away whether or not the
await
keyword is needed on an expression.var __requiringTry: Void
A value that abstracts away whether or not the
try
keyword is needed on an expression.func __checkBinaryOperation<T>(T?, (T?, () -> T?) -> T?, @autoclosure () -> T?, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) -> Result<T, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkBinaryOperation(String, (String, () -> String) -> Bool, @autoclosure () -> String, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) -> Result<Void, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkBinaryOperation<T, U>(T, (T, () -> U) -> Bool, @autoclosure () -> U, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) -> Result<Void, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkBinaryOperation<T>(T, (T, () -> T) -> Bool, @autoclosure () -> T, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) -> Result<Void, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkCast<V, T>(V, as: T.Type, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) -> Result<T, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkCast<V, T>(V, is: T.Type, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) -> Result<Void, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkClosureCall<R>(performing: () throws -> R, throws: (any Error) throws -> Bool, mismatchExplanation: ((any Error) -> String)?, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) -> Result<Void, any Error> Check that an expression always throws an error.
func __checkClosureCall<R>(performing: () async throws -> R, throws: (any Error) async throws -> Bool, mismatchExplanation: ((any Error) -> String)?, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) async -> Result<Void, any Error> Check that an expression always throws an error.
func __checkClosureCall(throws: Never.Type, performing: () throws -> some Any, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) -> Result<Void, any Error> Check that an expression never throws an error.
func __checkClosureCall(throws: Never.Type, performing: () async throws -> some Any, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) async -> Result<Void, any Error> Check that an expression never throws an error.
func __checkClosureCall<E>(throws: E, performing: () throws -> some Any, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) -> Result<Void, any Error> Check that an expression always throws an error.
func __checkClosureCall<E>(throws: E, performing: () async throws -> some Any, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) async -> Result<Void, any Error> Check that an expression always throws an error.
func __checkClosureCall<E>(throws: E.Type, performing: () throws -> some Any, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) -> Result<Void, any Error> Check that an expression always throws an error.
func __checkClosureCall<E>(throws: E.Type, performing: () async throws -> some Any, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) async -> Result<Void, any Error> Check that an expression always throws an error.
func __checkFunctionCall<T, Arg0, Arg1, Arg2, Arg3, R>(T, calling: (T, Arg0, Arg1, Arg2, Arg3) throws -> R?, Arg0, Arg1, Arg2, Arg3, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) rethrows -> Result<R, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkFunctionCall<T, Arg0, Arg1, Arg2, Arg3>(T, calling: (T, Arg0, Arg1, Arg2, Arg3) throws -> Bool, Arg0, Arg1, Arg2, Arg3, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) rethrows -> Result<Void, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkFunctionCall<T, Arg0, Arg1, Arg2, R>(T, calling: (T, Arg0, Arg1, Arg2) throws -> R?, Arg0, Arg1, Arg2, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) rethrows -> Result<R, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkFunctionCall<T, Arg0, Arg1, Arg2>(T, calling: (T, Arg0, Arg1, Arg2) throws -> Bool, Arg0, Arg1, Arg2, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) rethrows -> Result<Void, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkFunctionCall<T, Arg0, Arg1, R>(T, calling: (T, Arg0, Arg1) throws -> R?, Arg0, Arg1, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) rethrows -> Result<R, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkFunctionCall<T, Arg0, Arg1>(T, calling: (T, Arg0, Arg1) throws -> Bool, Arg0, Arg1, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) rethrows -> Result<Void, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkFunctionCall<T, each U, R>(T, calling: (T, repeat each U) throws -> R?, repeat each U, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) rethrows -> Result<R, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkFunctionCall<T, Arg0, R>(T, calling: (T, Arg0) throws -> R?, Arg0, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) rethrows -> Result<R, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkFunctionCall<T, each U>(T, calling: (T, repeat each U) throws -> Bool, repeat each U, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) rethrows -> Result<Void, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkFunctionCall<T, Arg0>(T, calling: (T, Arg0) throws -> Bool, Arg0, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) rethrows -> Result<Void, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkInoutFunctionCall<T, U, R>(T, calling: (T, inout U) throws -> R?, inout U, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) rethrows -> Result<R, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkInoutFunctionCall<T, U>(T, calling: (T, inout U) throws -> Bool, inout U, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) rethrows -> Result<Void, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkPropertyAccess<T, U>(T, getting: (T) -> U?, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) -> Result<U, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkPropertyAccess<T>(T, getting: (T) -> Bool, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) -> Result<Void, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkValue<T>(T?, expression: __Expression, expressionWithCapturedRuntimeValues: @autoclosure () -> __Expression?, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) -> Result<T, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __checkValue(Bool, expression: __Expression, expressionWithCapturedRuntimeValues: @autoclosure () -> __Expression?, mismatchedErrorDescription: @autoclosure () -> String?, difference: @autoclosure () -> String?, mismatchedExitConditionDescription: @autoclosure () -> String?, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) -> Result<Void, any Error> Check that an expectation has passed after a condition has been evaluated and throw an error if it failed.
func __ifMainActorIsolationEnforced<R>(@MainActor () async throws -> R, else: () async throws -> R
) async throws -> R Invoke a function isolated to the main actor if appropriate.
func __invokeXCTestCaseMethod<T>(__XCTestCompatibleSelector?, onInstanceOf: T.Type, sourceLocation: SourceLocation
) async throws -> Bool Run a test function as an
XCTestCase
-compatible method.func __invokeXCTestCaseMethod<T>(__XCTestCompatibleSelector?, onInstanceOf: T.Type, sourceLocation: SourceLocation
) async throws -> Bool Run a test function as an
XCTestCase
-compatible method.func __swiftPMEntryPoint(passing: __CommandLineArguments_v0?
) async -> CInt The entry point to the testing library used by Swift Package Manager.
func __swiftPMEntryPoint(passing: __CommandLineArguments_v0?
) async -> Never The entry point to the testing library used by Swift Package Manager.
func __xcTestCompatibleSelector(String
) -> __XCTestCompatibleSelector? Make an XCTest-compatible Objective-C selector from a string.
Show system interfaces (3)
Hide system interfaces
func __checkClosureCall(exitsWith: ExitCondition, performing: @convention(thin) () async -> Void, expression: __Expression, comments: @autoclosure () -> [Comment], isRequired: Bool, sourceLocation: SourceLocation
) async -> Result<Void, any Error> Check that an expression always exits (terminates the current process) with a given status.
func configurationForEntryPoint(from: __CommandLineArguments_v0
) throws -> Configuration Get an instance of
Configuration
given a sequence of command-line arguments passed from Swift Package Manager.func confirmation<R>(Comment?, expectedCount: some Confirmation.ExpectedCount, sourceLocation: SourceLocation, (Confirmation) async throws -> R
) async rethrows -> R Confirm that some event occurs during the invocation of a function.