XCTest

default testing framework, bundled with the Xcode IDE.

  • Test-Driven Development in Swift (2021)

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:

import XCTest
@testable import MyProject

// โญ๏ธ make a subclass of XCTestCase
class TestsForMyType: XCTestCase {

    /* -------- test case life cycle -------- */
    
    // โญ๏ธ runs before ALL tests
    override class func setUp() { ... }
    
    // โญ๏ธ runs after all tests
    override class func tearDown() { ... }
    
    // โญ๏ธ runs before each test
    override func setUpWithError() throws { ... }

    // โญ๏ธ runs after each test
    override func tearDownWithError() throws { ... }

    /* -------- test cases -------- */
 
    // โญ๏ธ method name starting with โ€œtestโ€
    func testThis() { } 
    func testThat() { } 

    // โญ๏ธ performance test case name starting with โ€œtestPerformanceโ€?
    func testPerformanceExample() throws {
        self.measure {
            // code to measure the time of
        }
    }
}

Last updated