XCTest is the default test harness on iOS an Apple’s other platforms. It provides support for organizing test cases and asserting expectations in your application code, and reporting the status of those expectations. It's not as fancy as some of the BDD frameworks like Quick and Cedar, but it has gotten much better than it used to be, and is my preferred test framework these days.
One place where the XCTest assertion utilities fall a bit short has been with managing Optional variables in swift. The default use of XCTAssert
don't provide any mechanism for unwrapping, easily leading to assertion checks like this:
class TestCaseDefault: XCTestCase {
func testAnOptional() {
let string: String? = nil
XCTAssertNotNil(string)
XCTAssert((string?.lengthOfBytes(using: .utf8))! > 0)
}
}
The asserts following XCTAssertNotNil
are where things can get ugly. It's very common for !
usage to sneak in, and force-unwrapping a nil
value will cause your tests to crash and bail early, meaning that the rest of your tests will not run.
A nice solution is possible, due to an often-overlooked feature of XCTestCase
. If the test function is marked with throws
, any thrown exceptions will cause the test to fail. We can use this to fail our tests using normal Swift flow control mechanisms:
class TestCaseThrows: XCTestCase {
struct UnexpectedNilVariableError: Error {}
func testAnOptional() throws {
let string: String? = nil
guard let newString = string else { throw UnexpectedNilVariableError() }
// newString is unwrapped, and things are happy
XCTAssert(newString.lengthOfBytes(using: .utf8) > 0)
}
}
This helps clean up our nil
check by using more typical swift Optional expressions. This will stop the test if it encounters nil
, and allows the remainder of the test to use an unwrapped value. This is a good start, except XCTestCase
doesn't report the error location correctly. If the test function throws, it does not point to the line that threw the exception. But not to worry: this is easy to clean up with a little wrapper that uses the #file
and #line
default values.
struct UnexpectedNilVariableError: Error {}
func UnwrapAndAssertNotNil<T>(_ variable: T?, message: String = "Unexpected nil variable", file: StaticString = #file, line: UInt = #line) throws -> T {
XCTAssertNotNil(variable, message, file: file, line: line)
guard let variable = variable else {
throw UnexpectedNilVariableError()
}
return variable
}
class TestCaseUnwrap: XCTestCase {
func testUnwrap() throws {
let string: String? = nil
let newString = try UnwrapAndAssertNotNil(string)
XCTAssert(newString.lengthOfBytes(using: .utf8) > 0)
}
}
Now we have a nice Swifty test helper to manage nil
checks. I hope this helps clean up your test code. And remember, !
is rarely a good answer, even in tests!