the Compartmented Robust Posix C++ Unit Test system |
|
---|
Verify that an expression throws the desired exception.
Used in: A test function body, the constructor or destructor of a
fixture, or a function called from
them.
See TEST(name, ...)
.
Requirement: expr;
can be used as a complete
statement.
Requirement: exc_type is either a type, or ....
The check succeeds if, and only if,
the statement expr;
throws an exception that can be caught using
catch(exc_type&).
On success the test function continues without side effects.
On failure the test is marked as failed, but continues execution
with an error report. The
report includes the expression text, the expression type and,
if the expression caught inherits from std::exception
the output string from the virtual function
std::exception::what()
, or a generic
message otherwise.
See also:
ASSERT_THROW(expr, exc_type)
Example: The test program
#include <crpcut.hpp> #include <vector> TEST(verify_throw_succeeds) { std::vector<int> v; VERIFY_THROW(v.at(3), std::out_of_range); } TEST(verify_throw_fails) { std::vector<int> v; VERIFY_THROW(v.at(3), std::domain_error); VERIFY_THROW(v.at(3), std::bad_alloc); } int main(int argc, char *argv[]) { return crpcut::run(argc, argv); }
reports one failed test with two violated
VERIFY_THROW
() checks.
FAILED!: verify_throw_fails fail--------------------------------------------------------------------------- samples/verify_throw_fails.cpp:40 VERIFY_THROW(v.at(3), std::domain_error) caught std::exception what()=vector::_M_range_check ------------------------------------------------------------------------------- fail--------------------------------------------------------------------------- samples/verify_throw_fails.cpp:41 VERIFY_THROW(v.at(3), std::bad_alloc) caught std::exception what()=vector::_M_range_check ------------------------------------------------------------------------------- phase="running" -------------------------------------------------------------- Earlier VERIFY failed ------------------------------------------------------------------------------- =============================================================================== 2 test cases selected Total : Sum Critical Non-critical PASSED : 1 1 0 FAILED : 1 1 0