kaycxx-test
C++ unit test framework
Loading...
Searching...
No Matches
assert_throw

Asserts that a function throws an exception of the expected type and expected message.

The expected exception type must derive from std::exception. Derived exception types match, like instanceof in JavaScript. Message checks use what() and can be exact string checks or regex checks.

#include <regex>
#include <stdexcept>
#include <kaycxx/test.hpp>
using namespace kaycxx::test;
int main() {
assert_throw<std::runtime_error>(
[] { throw std::runtime_error{"File not found"}; },
"File not found"
);
assert_throw<std::runtime_error>(
[] { throw std::runtime_error{"Error 404"}; },
std::regex{"Error [0-9]+"}
);
}
Unit test framework functions and types.
Definition test.hpp:15
Includes the complete public test API.

The assertion fails when the function does not throw, throws a different exception type, or throws the expected type with the wrong message.