www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Unittests and assert

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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.
:-/

(And I contend that compiling with both -release and -unittest is a
valid scenario: it lets you catch bugs that only show up in the release
version, such as accidentally relying on side-effects in asserts, which
work when compiling without -release, but mysteriously fail during
-release. Failing unittests would allow you to catch such mistakes
before you ship it to the irate customer.)


T

-- 
Computerese Irregular Verb Conjugation: I have preferences.  You have
biases.  He/She has prejudices. -- Gene Wirchenko
Feb 27 2013
next sibling parent "Andrej Mitrovic" <andrej.mitrovich gmail.com> writes:
On Thursday, 28 February 2013 at 04:52:01 UTC, H. S. Teoh wrote:
 Why? Because when you compile with -release -unittest, all 
 those asserts
 disappear, and the unittests become useless.

Are you sure? The asserts work for me: unittest { int getZero() { return 0; } assert(getZero != 0); } void main() { } $ dmd -release -unittest test.d $ test.exe
 core.exception.AssertError test(6): unittest failure

Feb 27 2013
prev sibling parent "Andrej Mitrovic" <andrej.mitrovich gmail.com> writes:
On Thursday, 28 February 2013 at 04:58:48 UTC, Andrej Mitrovic 
wrote:
 $ dmd -release -unittest test.d
 $ test.exe
 core.exception.AssertError test(6): unittest failure


Btw I think this might be an accident, in mars.c there's this code: if (global.params.release) { global.params.useInvariants = 0; global.params.useIn = 0; global.params.useOut = 0; global.params.useAssert = 0; // <-- note global.params.useArrayBounds = 1; global.params.useSwitchError = 0; } However then it's followed by this code: if (global.params.useUnitTests) global.params.useAssert = 1; So it's switched off and then on again. I can't tell if this was deliberate or not. If it's on purpose it should be documented.
Feb 27 2013