[Boost-users] [boost.test] How to register custom exception for auto test?

14 views
Skip to first unread message

Jörg Becker

unread,
Nov 6, 2012, 3:36:17 AM11/6/12
to boost...@lists.boost.org
Hi,

I'm using the test library of boost 1.49.0 for my unit tests. It works fine so
fare, but if my program throws an error, I get the default error message
unknown location(0): fatal error in "excption1_test": unknown type,
witch hides the original error message.

I tried to register an error translator, but without success. Here is what I
tried (modified test/test/custom_exception_test.cpp to use auto test with
global fixture):

#define BOOST_TEST_MODULE "boost test custom_exception_test"

#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_monitor.hpp>

using namespace boost::unit_test;

BOOST_AUTO_TEST_SUITE( custom_exception_testsuite )

struct my_exception1 {
explicit my_exception1( int res_code ) : m_res_code( res_code ) {}
int m_res_code;
};

void throw_my_exception1() {
throw my_exception1( 12 );
}

void my_exception1_translator( my_exception1 ) {
BOOST_TEST_MESSAGE( "Caught my_exception1" );
}

class my_config {
public:
my_config () {
unit_test_monitor.register_exception_translator<my_exception1>(
&my_exception1_translator );
}
};

BOOST_GLOBAL_FIXTURE( my_config )

BOOST_AUTO_TEST_CASE( excption1_test ) {
throw_my_exception1();
}

BOOST_AUTO_TEST_SUITE_END()



Can anybody tell me what I'm missing?

Thanks,
Jörg
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Gennadiy Rozenal

unread,
Nov 6, 2012, 10:36:50 AM11/6/12
to boost...@lists.boost.org
Jörg Becker <news <at> familiebecker.com> writes:

> I tried to register an error translator, but without success.

I've tried your example word for word and it appears to work as expected?
What do you observe?

Gennadiy

Jörg Becker

unread,
Nov 6, 2012, 12:43:07 PM11/6/12
to boost...@lists.boost.org
On Tuesday 06 November 2012 16:36:50 Gennadiy Rozenal wrote:
>
> I've tried your example word for word and it appears to work as expected?
> What do you observe?
>

I expect to see: Caught my_exception1

I get:
Running 1 test case...
unknown location(0): fatal error in "excption1_test": unknown type

Environment:
boost 1.49.0
gcc 4.6.3
Fedora 15
linked boost test library

Jörg

Gennadiy Rozental

unread,
Nov 8, 2012, 5:19:09 PM11/8/12
to boost...@lists.boost.org
Jörg Becker <news <at> familiebecker.com> writes:

> I expect to see: Caught my_exception1

This is what I observe with trunk build.



> I get:
> Running 1 test case...
> unknown location(0): fatal error in "excption1_test": unknown type
>
> Environment:
> boost 1.49.0
> gcc 4.6.3
> Fedora 15
> linked boost test library

Weird. Should have worked.

Gennadiy

Jörg Becker

unread,
Dec 19, 2013, 6:39:34 AM12/19/13
to boost...@lists.boost.org
On Thursday 08 November 2012 22:19:09 Gennadiy Rozental wrote:
> Jörg Becker <news <at> familiebecker.com> writes:
> > I expect to see: Caught my_exception1
>
> This is what I observe with trunk build.
>
> > I get:
> > Running 1 test case...
> > unknown location(0): fatal error in "excption1_test": unknown type
> >
> > Environment:
> > boost 1.49.0
> > gcc 4.6.3
> > Fedora 15
> > linked boost test library
>
> Weird. Should have worked.
>
> Gennadiy

Hello again!

I just gave boost 1.55.0 with gcc 4.7.2 a try. But the old test in my initial
mail still results in unknown location(0): fatal error in "excption1_test":
unknown type.

Since I was sure I saw the custom exception handling working one day, I tried
the header only version by including
#include <boost/test/included/unit_test.hpp>.
And this time I saw the expected result: Caught my_exception1!!!

Unfortunately I have to use the dynamic library test variant. So can anybody
tell me how to enable the custom exception handling for the dynamic test
library? What is the difference to the header-only variant (concerning the
exception handling).

Thanks for any hints
Jörg

Gennadiy Rozental

unread,
Jan 12, 2014, 9:55:45 PM1/12/14
to boost...@lists.boost.org
Jörg Becker <news <at> familiebecker.com> writes:

> Hello again!
>
> I just gave boost 1.55.0 with gcc 4.7.2 a try. But the old test in
> my initial mail still results in unknown location(0): fatal error

> in "exception1_test": unknown type.


>
> Since I was sure I saw the custom exception handling working one day,
> I tried the header only version by including
> #include <boost/test/included/unit_test.hpp>.
> And this time I saw the expected result: Caught my_exception1!!!
>
> Unfortunately I have to use the dynamic library test variant.
> So can anybody tell me how to enable the custom exception
> handling for the dynamic test
> library? What is the difference to the header-only variant
> (concerning the exception handling).

Hi Jörg,

I think the only significant difference is that library is build using
different build command. Maybe somehow it disables exceptions or something.
Otherwise I do not see a reason for this to happend. Let me know if you ever
figured out what was the root cause.

Regards,
Gennadiy

Reply all
Reply to author
Forward
0 new messages