Assertions for XCTest which prevent fatal errors causing the process to die.

Related tags

Testing Hela
Overview

Hela

Assertions for XCTest which prevent fatal errors causing the process to die.

The following assertions are supported. These functions are built on top of CwlPreconditionTesting which was created by Matt Gallagher of Cocoa with Love. That package allows for intercepting the signal from fatal errors to prevent the process from dying.

  • XCTAssertThrowFatalError
  • XCTAssertNoThrowFatalError

Usage

This package includes tests which show how these functions can be used. Note that the underlying package supports iOS and macOS only currently. For tests which are running on tvOS and watchOS an XCTSkip error will be thrown to indicate that the test was skipped so that it does not simply fail. These functions take a closure is run. The first one expects second one does not. If an fatal exception is raised the first assert function will pass and if not the second assert function will pass. These assert functions mirror the non-fatal error assert functions included with XCTest.

Why

Type safety can provide a lot of protection with support from the compiler making many bugs or invalid conditions to be impossible. Preconditions allow developers to impose strict requirements at runtime to compliment type safety. Apple prefers to have apps to be killed when it the problem is a result of programmer error. Placing preconditions in code to verify assumptions and enforce requirements can help developers understand why their code is not running properly long before it reaches production. Below is example of code which must run on the main thread and will sync to it with the requirement that it not already on the main thread. It may be necessary to do this work on the main thread before leaving the current code block and documentation would note that running this code should be off the main thread.

precondition(Thread.isMainThread, "Must not run on main thread")

DispatchQueue.main.sync {
    // run code on main threead
}

This precondition can also be handled by a special function from the Dispatch framework. One advantage of dispatchPrecondition is that you can also check if execution is on or off one of your own queues. The code below requires that execution is not running on the .main queue while it is running on the serialQueue.

dispatchPrecondition(condition: .notOnQueue(.main))
dispatchPrecondition(condition: .onQueue(serialQueue))

Both will not allow execution to continue if it is already operating on the main thread. If an app somehow manages to run this code on the main thread it will result in a runtime exception, crash the app and generate a crash report. That crash report will point directly to this line and show the code which lead up to the crash which is very helpful in diagnosing the crash. If the app continues running and crashes later it may be misleading and require more time to diagnose the problem.

Another common scenario is unwrapping self which was made weak with a capture list in an escaping closure. It is necessary to use self when referencing anything outside the scope of the closure and using weak self will break the potential retain cycle. Instead of risking the retain cycle many developers will do the weak self dance and immediate unwrap with a guard statement which requires returning from the else statement.

guard let self = self else { fatalError() }

Fortunately fatalError returns Never which means it will never return. It is a special type supported by the compiler which formalizes this scenario. Any function which returns Never will not return as the process should be terminated. It possible to use preconditionFailure in the same as it also returns Never. Both precondition and dispatchPrecondition do not return Never as they behave conditionally.

Assertions versus Preconditions

A key difference between an assert and precondition is that asserts are suppressed in release builds. Placing many asserts in your normal runtime code supports the development process by halting execution if any assertion fails to let a developer immediately see where the code has reached a bad state. Instead of setting breakpoints these assertions can stay in the code for ongoing development while being stripped out for release builds. A precondition can check an absolute requirement such as configuration which is necessary for the app to operate properly when that configuration should be included in the app bundle. It could also require that the Documents or Caches directory exists which it should unless something has seriously gone wrong. Triggering a crash and collecting crash reports will help with being aware of such problems in release builds. Ideally you will catch these problems with early testing before the app is available to users.

For test automation, asserts are a part of the Arrange, Act and Assert cycle.

Name

Why Hela? Hela is a Marvel character who is a necromancer who can raise the dead. Once a fatal error is raised normally a process will die. With help from Hela, the process can be raised from the dead and continue running your tests.


You might also like...
The Big List of Naughty Strings is a list of strings which have a high probability of causing issues when used as user-input data.
The Big List of Naughty Strings is a list of strings which have a high probability of causing issues when used as user-input data.

The Big List of Naughty Strings is a list of strings which have a high probability of causing issues when used as user-input data. I have put together

A wrapper around Foundation.Process, inspired by Rust's std::process::Command.

SwiftCommand A wrapper around Foundation.Process, inspired by Rust's std::process::Command. This package makes it easy to call command line programs a

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

JSONNeverDie - Auto reflection tool from JSON to Model, user friendly JSON encoder / decoder, aims to never die
JSONNeverDie - Auto reflection tool from JSON to Model, user friendly JSON encoder / decoder, aims to never die

JSONNeverDie is an auto reflection tool from JSON to Model, a user friendly JSON encoder / decoder, aims to never die. Also JSONNeverDie is a very important part of Pitaya.

Auto reflection tool from JSON to Model, user friendly JSON encoder / decoder, aims to never die
Auto reflection tool from JSON to Model, user friendly JSON encoder / decoder, aims to never die

JSONNeverDie is an auto reflection tool from JSON to Model, a user friendly JSON encoder / decoder, aims to never die. Also JSONNeverDie is a very imp

An extremely simple CLI tool that was created to diagnose and further understand an issue in DriverKit causing kIOHIDOptionsTypeSeizeDevice to behave incorrectly when used in DriverKit system extensions.

IOKitHIDKeyboardTester This tool is NOT useful to, or intended for general users. IOKitHIDKeyboardTester is an extremely simple (one-file!) CLI tool t

Codeless drop-in universal library allows to prevent issues of keyboard sliding up and cover UITextField/UITextView. Neither need to write any code nor any setup required and much more.
Codeless drop-in universal library allows to prevent issues of keyboard sliding up and cover UITextField/UITextView. Neither need to write any code nor any setup required and much more.

IQKeyboardManager While developing iOS apps, we often run into issues where the iPhone keyboard slides up and covers the UITextField/UITextView. IQKey

Validate iOS, Android, and Mac localizations. Find errors in .strings, .stringsdict, and strings.xml files.

Locheck An Xcode and Android localization file validator. Make sure your .strings, .stringsdict, and strings.xml files do not have any errors! What do

T - A simple testing framework using closures and errors

t Quickly test expectations What is t? t is a simple testing framework using clo

Prevent keyboard from covering UITextField/UITextView, includes Swift and Objective-C APIs

Prevent keyboard from covering UITextField/UITextView with only one line of code, includes Swift and Objective-C APIs.

A macOS menubar app to temporarily prevent the Mac from sleeping.

Caffeinate A macOS menubar app to temporarily prevent the Mac from sleeping. How does it work Left-clicking the cup will toggle Caffeinate. If the cup

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

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

A simple and lightweight matching library for XCTest framework.
A simple and lightweight matching library for XCTest framework.

Match A simple and lightweight matching library for XCTest framework. Getting started Swift Package Manager You can add Match to your project by addin

This repository accompanies Modularizing Legacy Projects Using TDD: Test Driven Development with XCTest for iOS
This repository accompanies Modularizing Legacy Projects Using TDD: Test Driven Development with XCTest for iOS

Apress Source Code This repository accompanies Modularizing Legacy Projects Using TDD: Test Driven Development with XCTest for iOS by Khaled El-Morabe

Styling and coloring your XCTest logs on Xcode Console
Styling and coloring your XCTest logs on Xcode Console

XLTestLog Notes with Xcode 8 and XLTestLog Since Xcode 8 killed XcodeColors, the current way using XCTestLog on Xcode 8 is just plain texts with emoji

Swift framework containing a set of helpful XCTest extensions for writing UI automation tests
Swift framework containing a set of helpful XCTest extensions for writing UI automation tests

AutoMate • AppBuddy • Templates • ModelGenie AutoMate AutoMate is a Swift framework containing a set of helpful XCTest extensions for writing UI autom

XCTestCrashDemo - XCTest Crash Demo with swift

XCTest Crash Demo This repo intends to centralize XCTest crash errors and the wa

An enhancement built on top of Foundation Framework and XCTest.

Beton is a Swift library built on top of the Foundation framework, that provides an additional layer of functionality, including easy localization, performance test measurement support, and convenience functionality. For us, Beton is primarily, but not exclusively, useful for server-side Swift engineering.

Comments
  • Support building into a dependency which fails due importing XCTest

    Support building into a dependency which fails due importing XCTest

    These build settings make the build work with an Xcode project but fails with a Swift package which just will not work the same way.

    ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES
    ENABLE_TESTING_SEARCH_PATHS = YES
    SYSTEM_FRAMEWORK_SEARCH_PATHS = $(CORRESPONDING_DEVICE_PLATFORM_DIR)/Developer/Library/Frameworks $(inherited)
    
    opened by brennanMKE 2
  • Change dependency to origin of CwlPreconditionTesting

    Change dependency to origin of CwlPreconditionTesting

    Once the issue is resolved this package can point directly to the original package. It currently points to a forked repo on a branch with a fix.

    https://github.com/mattgallagher/CwlPreconditionTesting/issues/21

    opened by brennanMKE 1
  • adds support to define a custom function for preconditionFailure

    adds support to define a custom function for preconditionFailure

    This custom function will support testing which won't depending on trapping the normal kill signal and will instead support returning a value which matches the return type while testing.

    opened by brennanMKE 0
Releases(0.0.4)
  • 0.0.4(May 5, 2022)

  • 0.0.3(Nov 28, 2021)

  • 0.0.2(Nov 28, 2021)

    Switches dependency back to the origin with the Apple Silicon fix. There is still the issue of importing XCTest as a library from a Swift package. With an Xcode project the following build settings can make it work but a solution for Swift packages has not been worked out yet.

    ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES
    ENABLE_TESTING_SEARCH_PATHS = YES
    SYSTEM_FRAMEWORK_SEARCH_PATHS = $(CORRESPONDING_DEVICE_PLATFORM_DIR)/Developer/Library/Frameworks $(inherited)
    
    Source code(tar.gz)
    Source code(zip)
  • 0.0.1(Nov 24, 2021)

Owner
Brennan Stehling
iOS Engineer, Objective-C and Swift
Brennan Stehling
The Big List of Naughty Strings is a list of strings which have a high probability of causing issues when used as user-input data.

The Big List of Naughty Strings is a list of strings which have a high probability of causing issues when used as user-input data. I have put together

Romain Pouclet 589 Sep 7, 2022
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

shindyu 22 Dec 1, 2022
T - A simple testing framework using closures and errors

t Quickly test expectations What is t? t is a simple testing framework using clo

OpenBytes 6 Nov 7, 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

Apple 1k Jan 4, 2023
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

Apress 57 Jan 1, 2023
A simple and lightweight matching library for XCTest framework.

Match A simple and lightweight matching library for XCTest framework. Getting started Swift Package Manager You can add Match to your project by addin

Michał Tynior 6 Oct 23, 2022
Swift framework containing a set of helpful XCTest extensions for writing UI automation tests

AutoMate • AppBuddy • Templates • ModelGenie AutoMate AutoMate is a Swift framework containing a set of helpful XCTest extensions for writing UI autom

PGS Software 274 Dec 30, 2022
XCTestCrashDemo - XCTest Crash Demo with swift

XCTest Crash Demo This repo intends to centralize XCTest crash errors and the wa

Omar Zúñiga 0 Jan 3, 2022
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

LinkedIn 565 Nov 16, 2022
Catching fatal errors in unit tests

Precondition Catching When running tests which hit fatal errors, often preconditions the built-in support with XCTest. One package which supports cach

Brennan Stehling 0 Nov 28, 2021