[Boost.Test] User Exception handling
Hello. On the webpage I read: In case if your test cases may throw custom exceptions (see here for list of exceptions translated by default), you could register translator specific for that exception. http://www.boost.org/libs/test/doc/components/utf/index.html So, if I understand it right, if I throw some custom exception in my test program and do not catch it, then it is catched by the boost test framework and reported in the proper way - if the exception can be translated by default. Now I look at the exceptions, which can be translated by default and see: In majority of the cases the monitored function doesn't need to throw the boost::execution_exception to report an error in the Execution Monitor. If you want a custom error message to be included into the execution_exception's error message, use one of the following C++ types as an exception: * C string. * std:string. * any exception class in std::exception hierarchy. http://www.boost.org/libs/test/doc/components/execution_monitor/index.html#c... So, I can use a C string for my exception. I have written a small example test case and included throwing of an uncaught exception in it: try{ throw "blub"; } catch(char* ex){ throw; } As I started the testing process I didn't get a "blub"-Exception message, but Boost.Test internal framework error: unknown reason So, what's wrong? Why is "blub" not reported as "blub"? Can anybody explain that? Regards Ewgenij
"Ewgenij Sokolovski"
I have written a small example test case and included throwing of an uncaught exception in it:
try{ throw "blub"; } catch(char* ex){ throw; }
As I started the testing process I didn't get a "blub"-Exception message, but
Boost.Test internal framework error: unknown reason
So, what's wrong? Why is "blub" not reported as "blub"? Can anybody explain that?
1. Please provide a complete example 2. What version of boost r u using? Gennadiy
1. Please provide a complete example
#include<iostream>
#include<vector>
#include
2. What version of boost r u using?
Version 1.33.1 Best regards Ewgenij Sokolovski -- GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS. Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail
"Ewgenij Sokolovski"
1. Please provide a complete example
#include<iostream>
#include<vector>
#include
#include using std::vector; using std::string;
using namespace boost::unit_test;
void myTestCase(vector<string> arguments) { string a = arguments[0]; }
test_suite* init_unit_test_suite(int argc, char** argv) {
throw "blub"; vector< vector<string> > arguments(1); arguments[0].push_back("haha"); test_suite* test = BOOST_TEST_SUITE("TestSuite"); test->add(BOOST_PARAM_TEST_CASE(&myTestCase,arguments.begin(), arguments.end())); return test; }
In this version I get the message:
Boost.Test internal framework error: unknown reason
But if I insert the throw "blub" into myTestCase-Function instead:
#include<iostream>
#include<vector>
#include
#include using std::vector; using std::string;
using namespace boost::unit_test;
void myTestCase(vector<string> arguments) { string a = arguments[0]; throw "blub"; }
test_suite* init_unit_test_suite(int argc, char** argv) {
vector< vector<string> > arguments(1); arguments[0].push_back("haha"); test_suite* test = BOOST_TEST_SUITE("TestSuite"); test->add(BOOST_PARAM_TEST_CASE(&myTestCase,arguments.begin(), arguments.end())); return test; }
Then the message after executing is:
Running 1 test case... unknown location(0): fatal error in "myTestCase": C string: blub
*** errors detected in test suite "TestSuite"; see standard output for details
So why it doesn't work, if throw is included in the main function?
Yes. At the moment init_unit_test_suite invocation is not guarded. I should probably change this. At the moment you've got 2 options: 1. Use global fixture (1.34.0) instead of init_unit_test_suite 2. use framework::setup_error to report initialization error
And why it is not possible to determine the location of the exception (the line number) when I include it into myTestCase?
Because C++ doesn't provide a way ;). Do you know one? Gennadiy
2. use framework::setup_error to report initialization error
Is it already in the 1.33.1 version? I couldn't find such a function.
And why it is not possible to determine the location of the exception (the line number) when I include it into myTestCase?
Because C++ doesn't provide a way ;). Do you know one?
Nope:) But I'm not a C++-guru, I'm only a poor computer science student at the beginning of my professional career:) -- GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS. Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail
On 6/29/07, Gennadiy Rozental
And why it is not possible to determine the location of the exception (the line number) when I include it into myTestCase?
Because C++ doesn't provide a way ;). Do you know one?
It might be possible to write custom code (per compiler / OS) to retrieve this info in debug. Maybe. Tony
"Gottlob Frege"
On 6/29/07, Gennadiy Rozental
wrote: And why it is not possible to determine the location of the exception (the line number) when I include it into myTestCase?
Because C++ doesn't provide a way ;). Do you know one?
It might be possible to write custom code (per compiler / OS) to retrieve this info in debug. Maybe.
Please feel free to submit patches to implement this improvement. Gennadiy
Ewgenij Sokolovski wrote: [SNIP]
http://www.boost.org/libs/test/doc/components/execution_monitor/index.html#c...
So, I can use a C string for my exception.
I have written a small example test case and included throwing of an uncaught exception in it:
try{ throw "blub"; } catch(char* ex){ throw; }
As I started the testing process I didn't get a "blub"-Exception message, but
Boost.Test internal framework error: unknown reason
So, what's wrong? Why is "blub" not reported as "blub"? Can anybody explain that?
Maybe "blub" was interpreted as "char[5]" or "char const *" instead of "char *". At least the first alternate choice is incompatible with "char *" as far as exception type matching is concerned. (The C family of languages is broken in that it treats arrays as pointers with funny settings. And even that breakage is broken because it uses that treatment only 90% of the time; the other 10% of uses treat arrays and pointers differently [e.g. extern], and you probably found one of them.) -- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT hotmail DOT com
(The C family of languages is broken in that it treats arrays as pointers with funny settings. And even that breakage is broken because it uses that treatment only 90% of the time; the other 10% of uses treat arrays and pointers differently [e.g. extern], and you probably found one of them.)
Might be a bit OT, but that reminds me of a classical paper from 1966 (Peter Landin, "The next 700 Programming Languages"): "For most programming languages there are certain statements of the kind, "There is a systematic equivalence between pieces of program like this, and pieces like that," that nearly hold but not quite." Just something that came to my mind.
participants (6)
-
Daryle Walker
-
Ewgenij Sokolovski
-
Ewgenij Sokolovski
-
Gennadiy Rozental
-
Gottlob Frege
-
KLINIK Markus STD2-G (AREVA NP GmbH)