The description and example code snippet is taken from Apple document.
For more information, see the Migrating a test from XCTest article.
- 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.
Type | XCTest | Swift Testing | |
---|---|---|---|
Import | import XCTest |
import Testing |
Example |
Test suite | class ExampleTests: XCTestCase |
struct ExampleTests |
Example |
Setup and teardown | class ExampleTests: XCTestCase |
struct ExampleTests |
Example |
Test method | @Test func example() |
func testExample() |
Example |
import XCTest
@testable import Example
import Testing
@testable import Example
class ExampleTests: XCTestCase { ... }
struct ExampleTests { ... }
// or
final class ExampleTests { ... }
class ExampleTests: XCTestCase {
var foo: NSNumber!
override func setUp() async throws {
foo = 100
}
override func tearDown() {
foo = 0
}
}
struct ExampleTests {
var foo: NSNumber
// setUp()
init() async throws {
foo = 100
}
// tearDown()
deinit {
foo = 0
}
}
// class test suite
final class ExampleTests {
var foo: NSNumber
// setUp()
init() async throws {
foo = 100
}
// tearDown()
deinit {
foo = 0
}
}
class ExampleTests: XCTestCase {
func testExample1() { ... }
func testExample2() async { ... }
func testExample3() throws { ... }
func testExample4() async throws { ... }
}
struct ExampleTests {
@Test func example1() { ... }
@Test func example2() async { ... }
@Test func example3() throws { ... }
@Test func example4() async throws { ... }
}