Skip to content
Wrappers for Swift assertions that take Error instances instead of strings, and the means to test them in XCTest.
Swift Ruby
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.


Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows Use the Swift version in the name of the check. Mar 16, 2020
Tests Split library into two CocoaPods so it compiles and runs successfully. Mar 10, 2020
.ruby-version CocoaPods support Mar 9, 2020
ErrorAssertions.podspec Bump version in podspec. Mar 16, 2020
muter.conf.json Remove duplicate muter setting. Mar 10, 2020


tests Documentation Version Swift Package Manager

Wrappers for Swift assertions that take Error instances instead of Strings, and a suite of test functions to test these assertions.

Getting Started

To use ErrorAssertions, simply import it at the top of your Swift source file:

import ErrorAssertions

Just by doing this, since the Swift compiler will prefer imported modules to the main Swift module, you’ll get the ErrorAssertion versions of functions like fatalError(_:file:line:).

Using Error Types

To use an Error instead of a String when calling an assertion method, use the error version:

import ErrorAssertions

doSomething(completionHandler: { error in
    if let error = error {

You can use Error types with fatalError(), assert(), assertionFailure(), precondition(), and preconditionFailure().

Testing Assertions

In your tests, import the ErrorAssertionExpectations module to test assertions made in your app (as long as you’ve imported ErrorAssertions). In your test cases, use the expectation methods:

func testThatAnErrorHappens() {
    expectFatalError {

There are also versions that take an Error or String and validate that the produced error is the one you’re expecting:

func testThatASpecificErrorHappens() {
    expectFatalError(expectedError: URLError.badURL) {


Swift Package Manager

Swift Package Manager is the preferred way to install ErrorAssertions. Add the repository as a dependency:

dependencies: [
    .package(url: "",
             from: "0.2.0")

In your targets, add ErrorAssertions as a dependency of your main target and, if you’re using the test support, add ErrorAssertionExpectations to the test target:

targets: [
    .target(name: "App", dependencies: ["ErrorAssertions")]
    .testTarget(name: "AppTests", dependencies: ["ErrorAssertionExpectations"])


To use ErrorAssertions with CocoaPods, use the main pod as a dependency in your app and the ErrorAssertionExpectations pod in your tests:

target 'App' do
    pod 'ErrorAssertions'

target 'AppTests' do
    pod 'ErrorAssertionExpectatoins'
You can’t perform that action at this time.