= Unittests = ''Part of'' TutorialFundamentals == Description == A unittest is a block of code that is ran before the execution of '''main()'''. You can enable the compilation and execution of unittests by passing the ''-unittest'' flag to DMD/RDMD, otherwise the unittests are not compiled and therefore not executed in runtime. A unittest is a way of grouping a collection of assert statements (and any other code), which allows you to easily test your code without worrying about performance implications (since you can disable the compilation of unittests alltogether). You can have multiple unittest blocks. Each unittest block will have its own scope, meaning that variables defined in one unittest do not conflict with variables defined in another unittest. The unittests are executed in the order they appear in the code. == Example == {{{ #!d import std.stdio; void main() { } class Foo { int x, y; } unittest { Foo foo = new Foo; foo.x = 2; foo.y = 4; assert(foo.x != foo.y, "this assert passes"); assert(foo.x < foo.y); } unittest { Foo foo = new Foo; foo.x = 2; foo.y = 2; assert(foo.x == foo.y); assert(foo.x > foo.y, "foo.x is not greater than foo.y!"); } }}} == Output == {{{ core.exception.AssertError@test.d(29): foo.x is not greater than foo.y! }}} == More Information == Compiling without the '''-unittest''' flag will skip compilation of unittest blocks. Note that you can still have assert statements outside of any unittest blocks, and these will always be compiled unless you put them in a unittest or version block. More info can be found in the [http://www.digitalmars.com/d/2.0/unittest.html D documentation].