C ++ Assertion to check that Exception is thrown

I know how the standard C ++ statement works. This was well reflected in my project for various testing purposes.

For example, suppose I want to verify that my code throws a specific exception.

Is it possible to do without using a testing infrastructure such as CPPUnit?

+7
source share
4 answers

You can do the same CPPUnit manually:

bool exceptionThrown = false; try { // your code } catch(ExceptionType&) // special exception type { exceptionThrown = true; } catch(...) // or any exception at all { exceptionThrown = true; } assert(exceptionThrown); // or whatever else you want to do 

Of course, if you use this template a lot, it makes sense to use a macro for this.

+6
source

skeleton (NOT TESTED)

 #define MY_ASSERT(f, e) {\ try {\ f();\ } catch (std::exception &e) {\ ...\ }\ } 
+2
source

Here is the exception tester that I wrote based on the answers of @JPlatte and @gongzhitaao. For my test environment, I have two global variables num_test and num_test_success that track how many tests are running and how many of them are successful in a series of tests, but this can be changed to suit your needs.

 int num_test; int num_test_success; #define UT_ASSERT_THROW(expression, ExceptionType) { \ \ try { \ expression; \ printf("test %d: *** FAILURE *** %s:%d:%s,%s\n", \ num_test, __FILE__, __LINE__, \ #expression, #ExceptionType); \ } catch (const ExceptionType &) { \ printf("test %d: success\n", num_test); \ ++num_test_success; \ } catch (...) { \ printf("test %d: *** FAILURE *** %s:%d:%s,%s\n", \ num_test, __FILE__, __LINE__, \ #expression, #ExceptionType); \ } \ \ ++num_test; \ \ } 

The __FILE__ and __LINE__ expand to the current file and line number (see https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html ). The pound signs inside the macro indicate to the preprocessor the place of the argument as a string in the macro extension (see http://bruceblinn.com/linuxinfo/Stringification.html ).

0
source

In-code statements are usually used at the beginning of a method to confirm that certain preconditions have been met before entering this particular function, for example:

 Window::paint() { assert(m_device != NULL); m_device->repaintRegion(); } 

they are here mainly to catch the errors of unmet dependencies between methods or classes.

assertions in test environments are different and are usually used for unit testing to make sure the device returned everything it needed to return.

exceptions should usually be thrown where reality (i.e. external systems) has provided us with a case that the code cannot / should not handle. its easy way out for rare, but still expected problems. for example, the timeout for the expected access to the server. or, not enough memory. I would not use it as an auxiliary for programming logic.

to your question, maybe there is a way to catch exceptions in test environments by surrounding the test block with try-catch. but I'm not sure if this really is.

NTN

-2
source

All Articles