Unit tests and verifying pre/post-conditions and invariants

Ali Çehreli acehreli at yahoo.com
Mon Aug 16 14:46:47 PDT 2010


Steven Schveighoffer wrote:
 > On Mon, 16 Aug 2010 12:57:22 -0400, Jonathan M Davis
 > <jmdavisprog at gmail.com> wrote:

 >> Of greater value is testing that normal exceptions are thrown when
 >> they're
 >> supposed to - especially for bad input. Testing that is essentially
 >> the same as
 >> testing for AssertErrors except that they're regular exceptions, so
 >> you don't
 >> have the whole issue with Errors not getting cleaned up properly. In
 >> the case of
 >> normal exceptions though, it is arguably part of the API (albeit not
 >> part of the
 >> signature), while for AssertError it's a contract which isn't really
 >> part of the
 >> API so much as verifying that your code is correct.
 >
 > I whole-heartedly agree, and the code I use for this is:
 >
 > bool caughtException = false;
 > try
 > {
 >    functionThatShouldThrow(badInput);
 > }
 > catch(SpecificException e)
 > {
 >    caughtException = true;
 > }
 > assert(caughtException);

I've used the same. :) We should have something like ensure_throws:

   ensure_throws!SpecificException(functionThatShouldThrow(badInput));

Perhaps some of the existing unit test libraries already have that.

Ali


More information about the Digitalmars-d-learn mailing list