Use TestableAssert to test if assertions in your code can catch programming errors properly
// TestLibrary.swift
func testWithAssert() {
assert(false, "assert() message")
}
func testWithPreconditionFailure() {
preconditionFailure("preconditionFailure() message")
}
func testWithFatalError() {
fatalError("fatalError() message")
}
func testWithRandomFailure() {
let testFuncs = [
testWithAssert,
testWithPreconditionFailure,
testWithFatalError,
]
guard let testFunc = testFuncs.randomElement() else {
fatalError()
}
testFunc()
}
// Tests.swift
expectFailedAssertion({ testWithAssert() })
expectFailedPrecondition({ testWithPreconditionFailure() }, "preconditionFailure() message")
expectFatalError({ testWithFatalError() })
expectAnyFailure({ testWithRandomFailure() })
Follow the official guide to add to your Xcode project
- Add the following package to the product target:
https://github.com/vinceplusplus/TestableAssertSupport
- Add the following package to the unit test target:
https://github.com/vinceplusplus/TestableAssert
- Add a configuration file to the product target to override built-in assertion functions:
// TestableAssertSupport+Configuration.swift import TestableAssertSupport func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) { AssertFuncBodies.assert(condition(), message(), file, line) } func assertionFailure(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) { AssertFuncBodies.assertionFailure(message(), file, line) } func precondition(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) { AssertFuncBodies.precondition(condition(), message(), file, line) } func preconditionFailure(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) -> Never { AssertFuncBodies.preconditionFailure(message(), file, line) } func fatalError(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) -> Never { AssertFuncBodies.fatalError(message(), file, line) }
- Add the following package dependencies:
.package(url: "https://github.com/vinceplusplus/TestableAssertSupport", from: "1.0.0"), .package(url: "https://github.com/vinceplusplus/TestableAssert", from: "1.0.0"),
- Add
TestableAssertSupport
as a dependency to the product target - Add
TestableAssert
as a dependency to the test target - Add a configuration file to the product target to override built-in assertion functions:
// TestableAssertSupport+Configuration.swift import TestableAssertSupport func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) { AssertFuncBodies.assert(condition(), message(), file, line) } func assertionFailure(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) { AssertFuncBodies.assertionFailure(message(), file, line) } func precondition(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) { AssertFuncBodies.precondition(condition(), message(), file, line) } func preconditionFailure(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) -> Never { AssertFuncBodies.preconditionFailure(message(), file, line) } func fatalError(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) -> Never { AssertFuncBodies.fatalError(message(), file, line) }