[Dune] Testing for exceptions

Lasse Hinrichsen l.hinrichsen at fu-berlin.de
Wed Apr 5 09:55:31 CEST 2017


Hello all,

I have a question about testing library code.
I want to test how an object/function/... handles an input that contains
a logic error (say, e.g., a negative value where only positive ones are
allowed). In my understanding such an object should throw an exception.
How do I properly test for such an behavior?

I currently use Dune::TestSuite. The following snippet

TestSuite suite;
try {
  foo(bad_input);
  suite.check(false, "Check if bad input throws") << "foo(bad_input) did
not lead to an exception";
  }
catch (...) {
  suite.check(true);
}

should work, however I don't find this very expressive (the suite itself
doesn't check anything). Is this a proper way to deal with this?

Best,
Lasse




More information about the Dune mailing list