Test assertions and preconditions.
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.
To configure your System under Test, you just need to import the RuntimeAssertion
library and call your runtime assertions functions as usual.
import RuntimeAssertions
func foo() {
precondition(someFooCondition, "Foo condition is unmet.")
// ...
}
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:
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:
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.
Import the RuntimeAssertionsTesting
module if you use Swift Testing; import XCTRuntimeAssertions
if you use XCTest.
Important: Don't import
RuntimeAssertionsTesting
orXCTRuntimeAssertions
in your application target.
Contributions to this project are welcome. Please make sure to read the contribution guidelines and the contributor covenant code of conduct first.
This project is licensed under the MIT License. See Licenses for more information.