Ddoc $(SPEC_S $(TITLE), $(GRAMMAR $(GNAME UnitTest): $(B unittest) $(I FunctionBody) ) $(P Unit tests are a series of test cases applied to a module to determine if it is working properly. Ideally, unit tests should be run every time a program is compiled. ) $(P Unit tests are a special function defined like:) ------ unittest { ...test code... } ------ $(P There can be any number of unit test functions in a module, including within struct, union and class declarations. They are executed in lexical order. Stylistically, a unit test for a function should appear immediately following it. ) $(P A compiler switch, such as $(LINK2 dmd-windows.html#switches, $(B -unittest)) for $(B dmd), will cause the unittest test code to be compiled and incorporated into the resulting executable. The unittest code gets run after static initialization is run and before the $(TT main()) function is called. ) $(P For example, given a class Sum that is used to add two values:) ------ class Sum { int add(int x, int y) { return x + y; } unittest { Sum sum = new Sum; assert(sum.add(3,4) == 7); assert(sum.add(-2,0) == -2); } } ------

Versioning

$(P The $(LINK2 version.html#PredefinedVersions, version identifier) $(B unittest) is predefined if the compilation is done with unit tests enabled. ) ) Macros: TITLE=Unit Tests WIKI=UnitTests GLINK=$(LINK2 #$0, $(I $0)) GNAME=$(I $0) DOLLAR=$ FOO=