= Unittests = ''Part of'' TutorialFundamentals == Description == Unittests let you find the bugs before you even run the program. == About unittests == According to this example, unittests may be run in the order they appear in the code. Since the Whatever class appears before the module unittests, these are run first. The -unittests flag has to be used to compile this program or else this code is ignored. == Example == {{{ #!d module unittests; import std.stdio; void main() { writefln("test"); /* This assertion will fail. */ assert(false); /* The line number (7) may be different, but this is the error message which should appear... Error: AssertError Failure unittests(7) */ } class Whatever { unittest { writefln("class Whatever UnitTest..."); /* If the expession of an assert doesn't evaluate to true, an exception is thrown at run-time. */ assert(true==1); assert(true); assert(false==false); assert(!false); } } unittest { writefln("module UnitTest..."); assert(true==1); assert(true); assert(false==false); } }}} == Example 2 == {{{ #!d module unittests; import std.stdio; void main() { writefln( "Passes!"); return; } class Factoid { int factorial() { int x = 1; int f; for( x = 0 ; x <= m_int; ++x) { if( x == 0) { f = 1; } else { f *= x; } } return f; } this(int x) { m_int = x; } private{ int m_int; } unittest{ Factoid f = new Factoid(3); assert( 6 == f.factorial()); } } }}}