Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing exceptions with BOOST

I'm using boost test framework 1.47 and I'm having difficulties testing my exceptions

Here is my exception class

class VideoCaptureException : public std::exception
{

    std::string m_Description;
public:
    VideoCaptureException(const char* description)
    {
        m_Description = std::string(description);
    }
    VideoCaptureException(const std::string& description)
    {
        m_Description = description;
    }
    virtual ~VideoCaptureException() throw() {}
    virtual const char* what() const throw()
    {
        return m_Description.c_str();
    }
}

I'm trying to test code that simply throws this exception

BOOST_CHECK_THROW( source.StopCapture(), VideoCaptureException )

For some reason it doesn't work.

unknown location(0): fatal error in "testVideoCaptureSource": unknown type
testVideoCaptureSource.cpp(28): last checkpoint

What is it that I'm doing wrong?

like image 233
Eric Avatar asked Nov 06 '11 22:11

Eric


People also ask

What are exceptions in testing?

Technically exception is an object thrown at run time. In software testing we skip the statements which has error and resume to further test execution.

What is boost testing?

Boost unit testing framework (Boost. Test) is a part of the Boost library. It is a fully-functional and scalable framework, with wide range of assertion macros, XML output, and other features. Boost. Test itself lacks mocking functionality, but it can be combined with stand-alone mocking frameworks such as gmock.

What is test adapter for boost test?

The Test Adapter for Boost. Test is a unit testing extension published by Microsoft and based on the existing Boost Unit Test Adapter (v1. 0.8) Visual Studio extension by Gunter Wirth's team from ETAS GmbH. This extension is developed in collaboration with the original project with the aim of improving Boost.

How do I install a boost test?

Create a Boost. cpp file for your tests, right-click on the project node in Solution Explorer and choose Add > New Item. In the Add New Item dialog, expand Installed > Visual C++ > Test. Select Boost. Test, then choose Add to add Test.


1 Answers

After encountering this error myself, I have tracked it down to a silly, but easy-to-make mistake:

throw new VideoCaptureException( "uh-oh" );

will fail with that error message, while:

throw VideoCaptureException( "uh-oh" );

will succeed.


The new variant causes a pointer to an exception to be caught, rather than the exception itself. The boost library doesn't know what to do with this, so it just says "unknown type".

It would be nice if the library explained the situation properly, but hopefully anybody else hitting "fatal error: unknown type" will find this page and see how to fix it!

like image 61
Dave Avatar answered Oct 07 '22 14:10

Dave