Skip to content

StanfordBDHG/XCTRuntimeAssertions

Repository files navigation

XCTRuntimeAssertions

Build and Test codecov DOI

Test assertions and preconditions using XCTest.

Overview

This package allows developers to test assertions and preconditions in tests using XCTest. This is done by overloading Swifts runtime assertions with functions provided by XCTRuntimeAssertions. These are always called in your System under Test. Only if requested within a unit test, their implementations are swapped to assert a runtime assertion.

Configure your System under Test

To configure your System under Test, you just need to import the XCTRuntimeAssertion package and call your runtime assertions functions as usual.

import XCTRuntimeAssertions

func foo() {
    precondition(someFooCondition, "Foo condition is unmet.")
    // ...
}

Testing Runtime Assertions

In your unit tests you can use the XCTRuntimeAssertion and XCTRuntimePrecondition functions to test a block of code for which you expect a runtime assertion to occur.

Below is a short code example demonstrating this for assertions:

try XCTRuntimeAssertion {
    assertionFailure()
}

Below is a short code example demonstrating this for preconditions:

try XCTRuntimePrecondition {
    preconditionFailure()
}

Tip

XCTRuntimeAssertion and XCTRuntimePrecondition also support the execution of async code.

Contributing

Contributions to this project are welcome. Please make sure to read the contribution guidelines and the contributor covenant code of conduct first.

License

This project is licensed under the MIT License. See Licenses for more information.

Stanford Byers Center for Biodesign Logo Stanford Byers Center for Biodesign Logo