Skip to content

Instantly share code, notes, and snippets.

@seonar22
Forked from KingOfBrian/XCTestAndNil.md
Created December 2, 2023 13:48
Show Gist options
  • Save seonar22/80ba6f6b3185d3edb22ed0b32496b651 to your computer and use it in GitHub Desktop.
Save seonar22/80ba6f6b3185d3edb22ed0b32496b651 to your computer and use it in GitHub Desktop.
XCTest and nil assertions

XCTest and nil

XCTest is the default test harness on iOS. 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, and is my preferred test framework these days.

The Problem

One place where the XCTest assertion utilites fall a bit short has been with managing Optional variables in swift. The default use of XCTAssert don't provide any mechanism for un-wrapping, 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 real ugly. It's very common for ! usage to sneak in, and cause your tests to crash after a failure, which causes the rest of your tests to not run.

The Solution

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 really helps clean up the usage of our XCTAssertNotNil with a bit more typical swift Optional usage, and stops the test if nil is encountered. This is pretty nice, except that XCTestCase doesn't report the error location correctly, which is less than ideal. Not to worry, this is easy to clean up with 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment