https://github.com/stanfordbdhg/xctruntimeassertions
XCTest extensions to test runtime assertions and preconditions
https://github.com/stanfordbdhg/xctruntimeassertions
assertion fatalerror precondition runtime stanford swift test xcode xctest
Last synced: about 1 month ago
JSON representation
XCTest extensions to test runtime assertions and preconditions
- Host: GitHub
- URL: https://github.com/stanfordbdhg/xctruntimeassertions
- Owner: StanfordBDHG
- License: mit
- Created: 2023-03-08T01:11:00.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-16T22:27:38.000Z (2 months ago)
- Last Synced: 2025-04-12T12:00:06.740Z (about 1 month ago)
- Topics: assertion, fatalerror, precondition, runtime, stanford, swift, test, xcode, xctest
- Language: Swift
- Homepage: https://swiftpackageindex.com/StanfordBDHG/XCTRuntimeAssertions/documentation/xctruntimeassertions
- Size: 57.6 KB
- Stars: 2
- Watchers: 13
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Citation: CITATION.cff
Awesome Lists containing this project
README
# RuntimeAssertions
[](https://github.com/StanfordBDHG/XCTRuntimeAssertions/actions/workflows/build-and-test.yml)
[](https://codecov.io/gh/StanfordBDHG/XCTRuntimeAssertions)
[](https://doi.org/10.5281/zenodo.7800545)
[](https://swiftpackageindex.com/StanfordBDHG/XCTRuntimeAssertions)
[](https://swiftpackageindex.com/StanfordBDHG/XCTRuntimeAssertions)Test assertions and preconditions.
## Overview
This library provides the necessary runtime support to support unit testing assertions and preconditions.
The library overloads Swifts runtime assertions:
* `assert(_:_:file:line:)`
* `assertionFailure(_:file:line:)`
* `precondition(_:_:file:line:)`
* `preconditionFailure(_:file:line:)`Always call this method in your System under Test.
Only if requested within a unit test, their implementations are swapped to assert a runtime assertion.
Release builds will completely optimize out this runtime support library and direct calls to the original Swift implementation.### Configure your System under Test
To configure your System under Test, you just need to import the `RuntimeAssertion` library and call your runtime assertions functions as usual.
```swift
import RuntimeAssertionsfunc foo() {
precondition(someFooCondition, "Foo condition is unmet.")
// ...
}
```### Testing Runtime Assertions
In your unit tests you can use the `expectRuntimeAssertion(expectedCount:_:assertion:sourceLocation:_:)` and
`expectRuntimePrecondition(timeout:_:precondition:sourceLocation:_:)` 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:
```swift
import RuntimeAssertionsTesting
import Testing@Test
func testAssertion() {
expectRuntimeAssertion {
// code containing a call to assert() of the runtime support ...
}
}
```Below is a short code example demonstrating this for preconditions:
```swift
import RuntimeAssertionsTesting
import Testing@Test
func testPrecondition() {
expectRuntimePrecondition {
// code containing a call to precondition() of the runtime support ...
}
}
```> Tip: Both expectation methods also support the execution of `async` code.
## Contributing
Contributions to this project are welcome. Please make sure to read the [contribution guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md) and the [contributor covenant code of conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md) first.
## License
This project is licensed under the MIT License. See [Licenses](https://github.com/StanfordBDHG/XCTRuntimeAssertions/tree/main/LICENSES) for more information.

