Categories
Dev

Tip: Make XCTestCase tests throw

Here’s a quick tip so you don’t need to try! or do { ... } catch { ... } so much in unit tests:

Mark your test functions with throws.

import XCTest

class ModelTests: XCTestCase {

    func testSave() throws {
        let model = Model()
        try model.save()
        XCTAssert(...)
    }

}

No ugly exclamation marks, and if an error is thrown the test still fails and the error is logged to the test console.