Unittests and assert

Jonathan M Davis jmdavisProg at gmx.com
Wed Feb 27 20:57:57 PST 2013


On Wednesday, February 27, 2013 20:50:02 H. S. Teoh wrote:
> I just had a sinking realization today: I have been using assert inside
> unittests to check for test results, but actually, this is wrong!
> 
> Why? Because when you compile with -release -unittest, all those asserts
> disappear, and the unittests become useless.  The correct way is to use
> enforce instead of assert.
> 
> Unfortunately, Phobos unittests are full of asserts rather than enforce.

Actually, building with -release doesn't disable assertions if you build with 
-unittest. This code will assert if you bulid with both -release and -
unittest:

import std.datetime;

void main()
{
    assert(TimeOfDay(12, 7, 5).toString() == "foo");
}

So, while I can understand why you'd think that we have a problem, we actually 
don't.

- Jonathan M Davis


More information about the Digitalmars-d mailing list