the Compartmented Robust Posix C++ Unit Test system |
|
---|
A test modifier which states that the normal way to finish the test is via an exception instead of returning from the function.
Used in: The modifier list of a test.
See TEST(name, ...)
The parameter to
EXPECT_EXCEPTION
(type) is the
type of the expected exception, or ...
if any
type is accepted.
If the test finishes through any other means, it fails. If it leaves
through another exception, inheriting from std::exception,
the output from the virtual member function
std::exception::what()
is included in the error
report.
Note | |
---|---|
EXPECT_EXCEPTION(type) cannot be used together with
either of EXPECT_EXIT(num, action?) or
EXPECT_SIGNAL_DEATH(signo, action?) .
|
Example: The test program
#include <crpcut.hpp> #include <vector> TEST(fail_no_exception, EXPECT_EXCEPTION(...)) { } TEST(pass_any_exception, EXPECT_EXCEPTION(...)) { std::vector<int> v; v.at(3); } TEST(fail_wrong_exception, EXPECT_EXCEPTION(std::domain_error)) { std::vector<int> v; v.at(3); } TEST(pass_correct_exception, EXPECT_EXCEPTION(std::out_of_range)) { std::vector<int> v; v.at(3); } int main(int argc, char *argv[]) { return crpcut::run(argc, argv); }
reports two failed tests:
FAILED!: fail_no_exception phase="running" -------------------------------------------------------------- samples/expect_exception.cpp:31 Unexpectedly did not throw ------------------------------------------------------------------------------- =============================================================================== FAILED!: fail_wrong_exception phase="running" -------------------------------------------------------------- samples/expect_exception.cpp:41 Unexpectedly caught std::exception what()=vector::_M_range_check: __n (which is 3) >= this->size() (which is 0) ------------------------------------------------------------------------------- =============================================================================== 4 test cases selected Sum Critical Non-critical PASSED : 2 2 0 FAILED : 2 2 0