Test assertions and preconditions using XCTest.
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.
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.")
// ...
}
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.
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.