Test Library for Swift's Error Handling

Last update: May 16, 2022

CatchingFire

License: MIT CocoaPods

CatchingFire is a Swift test framework, which helps making expectations against the error handling of your code. It provides for this purpose two higher-order functions, which take throwing functions and check whether the given closure throws or not. It integrates seamlessly with the expecters provided by XCTest.

Usage

AssertNoThrow

AssertNoThrow allows you to write safe tests for the happy path of failable functions. It helps you to avoid the try! operator in tests.

If you want to test a function, which may fail in general, you may think of using try. But this would mean that you have to declare your test method as throwing, which causes that XCTest doesn't execute the test anymore.

So in consequence, you would usually need to write:

XCTAssertEqual(try! fib(x), 21)

If the expression fails, your whole test suite doesn't execute further and aborts immediately, which is very undesirable, especially on CI, but also for your workflow when you use TDD.

Instead you can write now:

AssertNoThrow {
    XCTAssertEqual(try fib(x), 21)
}

Or alternatively:

AssertNoThrow(try fib(x)).map { (y: Int) in
    XCTAssertEqual(y, 21)
}

If the expression fails, your test fails.

AssertThrow

AssertThrow allows to easily write exhaustive tests for the exception paths of failable functions. It helps you to avoid writing the same boilerplate code over and over again for tests.

If you want to test a function, that it fails for given arguments, you would usually need to write:

do {
    try fib(-1)
    XCTFail("Expected to fail, but did not failed!")
} catch Error.ArgumentMayNotBeNegative {
    // succeed silently
} catch error {
    XCTFail("Failed with a different error than expected!")
}

Instead you can write now:

AssertThrow(Error.ArgumentMayNotBeNegative) {
    try fib(-1)
}

If the expression or closure doesn't throw the expected error, your test fails.

Installation

CatchingFire is available through CocoaPods. To install it, simply add it to your test target in your Podfile:

use_frameworks!
target "AppTest" do
  pod 'CatchingFire'
end

Author

Marius Rackwitz, [email protected]
Find me on Twitter as @mrackwitz.

License

Version is available under the MIT license. See the LICENSE file for more info.

GitHub

https://github.com/mrackwitz/CatchingFire
You might also like...

test ios UnitTest and UITest

github_actions Bundlerの導入 fastlaneやiOSパッケージマネージャであるCocoaPodsはRubyのライブラリ 開発チームで使用するバージョンを揃えるためにBundlerを導入する bundlerのインストール gem install bundler Gemfile

Nov 3, 2021

Test task application based on Swift using CoreData, Alamofire, AlamofireImage and CocoaPods

Test task application based on Swift using CoreData, Alamofire, AlamofireImage and CocoaPods

iTunes Search Test task application based on Swift using CoreData, Alamofire, AlamofireImage and CocoaPods Features 🔍 Searching music albums by name

Oct 31, 2021

Test Technique pour LeBonCoin

Test Technique pour LeBonCoin Test Technique pour LeBonCoin Créer une application universelle (iPhone, iPad) en Swift. Celle-ci devra afficher une lis

Nov 3, 2021

iOS Test Hasitha

Welcome to the Bidone iOS Coding Test! iOS Create a simple application with the list of orders from viewModel and display them in a list. When a user

Nov 12, 2021

iOS Test for Openbank by David Moreno Lora

iOS Test for Openbank by David Moreno Lora

MarvelMobileTest-iOS iOS Test for Openbank by David Moreno Lora Installation Clone the project and install the dependencies using pod install Once th

Nov 16, 2021

Trade Me Technical Test

Trade Me Technical Test

TradeMeTechnicalTest Chris Samuels This repository contains a Xcode project. It is for submission to Trade Me Mobile Technical Test. The Application i

Nov 25, 2021

Write unit tests which test the layout of a view in multiple configurations

Write unit tests which test the layout of a view in multiple configurations

Overview This library enables you to write unit tests which test the layout of a view in multiple configurations. It tests the view with different dat

May 30, 2022

A collection of useful test helpers designed to ease the burden of writing tests for iOS applications.

A collection of useful test helpers designed to ease the burden of writing tests for iOS applications.

MetovaTestKit is a collection of useful test helpers designed to ease the burden of writing tests for iOS applications. Requirements Installation Usag

Aug 29, 2021

XCTestExtensions is a Swift extension that provides convenient assertions for writing Unit Test.

XCTestExtensions is a Swift extension that provides convenient assertions for writing Unit Test.

XCTestExtensions Features XCTAssertEventually (that convenient assertions for writing Unit Test). Use "XCTAssertEventually", you can write asynchronou

Feb 28, 2022
Comments
  • 1. Show failures on same line as caller

    Failure messages are now shown on the line that initially used the assertion, not buried in the framework code.

    This is done by setting default file and line parameters to each assertion method. The default for these options are __FILE__ and __LINE__, respectively, which come from Swift's implicitly defined constants. See the image below for an example.

    screen shot 2015-12-27 at 2 04 04 pm

    More information can be found in this blog post, Custom Helpers in XCTest.

    Reviewed by joemasilotti at 2015-12-27 19:11
Related tags
Kfm-ios-test - Test online for iOS Developer position in Kimia Farma or PT. Buana Varia Komputama
Kfm-ios-test - Test online for iOS Developer position in Kimia Farma or PT. Buana Varia Komputama

kfm-ios-test Kimia Farma Mobile iOS Test Test online for iOS Developer position

Feb 23, 2022
The XCTest Project, A Swift core library for providing unit test support

XCTest The XCTest library is designed to provide a common framework for writing unit tests in Swift, for Swift packages and applications. This version

Jun 15, 2022
A Swift test double library. Guava - looks like an apple but it's not.

Guava Guava helps you to make your unit tests more flexible. It allows you to replace parts of your system under test with a test double objects. Tabl

Mar 22, 2022
Trust - Swift DCI Test Library

Trust Swift - DCI Pattern Test Library Support SPM How to use ? struct User {

Feb 13, 2022
Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!
Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!

OHHTTPStubs OHHTTPStubs is a library designed to stub your network requests very easily. It can help you: test your apps with fake network data (stubb

Jun 23, 2022
BDD Framework and test runner for Swift projects and playgrounds
BDD Framework and test runner for Swift projects and playgrounds

Spectre Special Executive for Command-line Test Running and Execution. A behavior-driven development (BDD) framework and test runner for Swift project

Jun 6, 2022
This repository accompanies Test-Driven Development in Swift: Compile Better Code with XCTest and TDD
This repository accompanies Test-Driven Development in Swift: Compile Better Code with XCTest and TDD

Apress Source Code This repository accompanies Test-Driven Development in Swift: Compile Better Code with XCTest and TDD by Gio Lodi (Apress, 2021). D

May 23, 2022
This is a simple test app getting data from network to practice a tad bit.

test This is a simple test app getting data from network to practice a tad bit. Start Nothing fancy, no CocoaPods, just clone and run! Architecture Ju

Oct 9, 2021
Test case project for mackolik

Mackolik - iOS Developer - Test Case Gökhan Mandacı 28 Oct 2021 I developed a two-page app and a general purpose drop down widget for the Mackolik Tes

Oct 28, 2021
This repo holds the code for Dubizzle & Bayut test App

DubizzleClassified This repo holds the code for Dubizzle & Bayut test App About App This is a simple app which basically fetches item list from the gi

Jun 2, 2022