default testing framework, bundled with the Xcode IDE.
Test-Driven Development in Swift (2021)
help
XCTestCase offers methods to run code before each test or all the tests will run, as well as after each test or all the tests did run:
importXCTest@testableimportMyProject// ⭐️ make a subclass of XCTestCaseclassTestsForMyType:XCTestCase {/* -------- test case life cycle -------- */// ⭐️ runs before ALL testsoverrideclassfuncsetUp() { ... }// ⭐️ runs after all testsoverrideclassfunctearDown() { ... }// ⭐️ runs before each testoverridefuncsetUpWithError() throws { ... }// ⭐️ runs after each testoverridefunctearDownWithError() throws { ... }/* -------- test cases -------- */// ⭐️ method name starting with “test”functestThis() { } functestThat() { } // ⭐️ performance test case name starting with “testPerformance”?functestPerformanceExample() throws { self.measure {// code to measure the time of } }}
XCTAssertTrue(true)
XCTAssertFalse(false)
compare the values of types conforming to Comparable:
XCTAssertLessThan(1, 2)
XCTAssertGreaterThan(2, 1)
XCTAssertLessThanOrEqual(2, 2)
XCTAssertGreaterThanOrEqual(2, 1)
XCTAssertNil(nil)
XCTAssertNotNil("this is not nil")
// will fail if user == nil, or firstName !== "John"XCTAssertEqual(user?.firstName, "John")// use `XCTUnwrap` insteadlet u =tryXCTUnwrap(user)// fail if user == nilXCTAssertEqual(u.firstName, "John")// fail if not equal