How can I catch my usual exception with Boost.Test?

When I test my C ++ class with Boost.Test and change my own custom exceptions (these are instances of my class), this is the message I see in the log:

unknown location:0: fatal error in "testMethod": unknown type

This is very uninformative, and I don’t know how to teach Boost.Test to convert my exception to a string and display it correctly. My class Exceptionhas operator string(), but that doesn't help. Any ideas? Thank!

+3
source share
4 answers

I believe this will work if your own exception class is inherited from std::exception.

+2
source

You may need to define the <<operator in the std namespace:

namespace std {
    inline std::ostream& operator<<(std::ostream& os, const Exception& ex) {
        os << ex.string();
        return os;
    }
}

boost.test .

, BOOST_CHECK_EQUAL() ..

+1

You can check if the function is selected by the specified, except by using BOOST_CHECK_THROW or similar

see Boost.Test Docs :

class my_exception{};

BOOST_AUTO_TEST_CASE( test )
{
   int i =  0;
   BOOST_CHECK_THROW( i++, my_exception );
}
+1
source

I just inherited it from std::stringand everything works fine.

0
source

Source: https://habr.com/ru/post/1754843/


All Articles