$(DDOC $(DDOC_BLANKLINE ) $(DDOC_BLANKLINE ) $(SPEC_S Unit Tests, $(DDOC_BLANKLINE ) $(HEADERNAV_TOC $(HEADERNAV_ITEM attributes_unittest, Attributed Unittests) $(HEADERNAV_ITEM documented-unittests, Documented Unittests) ) $(DDOC_BLANKLINE ) $(GRAMMAR $(GNAME UnitTest): $(D unittest) $(GLINK2 statement, BlockStatement) ) $(P Unit tests are a builtin framework of test cases applied to a module to determine if it is working properly. A D program can be run with unit tests enabled or disabled. ) $(DDOC_BLANKLINE ) $(P Unit tests are a special function defined like:) $(DDOC_BLANKLINE ) $(D_CODE $(D_KEYWORD unittest) { ...test code... } ) $(DDOC_BLANKLINE ) $(P Individual tests are specified in the unit test using $(DDSUBLINK spec/expression, AssertExpression, AssertExpressions). Unlike $(I AssertExpression)s used elsewhere, the assert is not assumed to hold, and upon assert failure the program is still in a defined state. ) $(DDOC_BLANKLINE ) $(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. ) $(DDOC_BLANKLINE ) $(P Unit tests, when enabled, are run after all static initialization is complete and before the $(D main()) function is called. ) $(DDOC_BLANKLINE ) $(P For example, given a class $(D Sum) that is used to add two values, a unit test can be given:) $(DDOC_BLANKLINE ) $(D_CODE $(D_KEYWORD class) Sum { $(D_KEYWORD int) add($(D_KEYWORD int) x, $(D_KEYWORD int) y) { $(D_KEYWORD return) x + y; } $(D_KEYWORD unittest) { Sum sum = $(D_KEYWORD new) Sum; $(D_KEYWORD assert)(sum.add(3,4) == 7); $(D_KEYWORD assert)(sum.add(-2,0) == -2); } } ) $(DDOC_BLANKLINE ) $(P When unit tests are enabled, the $(DDSUBLINK spec/version, PredefinedVersions, version identifier) $(D unittest) is predefined. ) $(DDOC_BLANKLINE ) $(DDOC_BLANKLINE )

$(LNAME2 attributes_unittest, Attributed Unittests)

$(DDOC_BLANKLINE ) $(P A unittest may be attributed with any of the global function attributes. Such unittests are useful in verifying the given attribute(s) on a template function:) $(DDOC_BLANKLINE ) $(D_CODE $(D_KEYWORD void) myFunc(T)(T[] data) { $(D_KEYWORD if) (data.length > 2) data[0] = data[1]; } @safe $(D_KEYWORD nothrow) $(D_KEYWORD unittest) { $(D_KEYWORD auto) arr = [1,2,3]; myFunc(arr); $(D_KEYWORD assert)(arr == [2,2,3]); } ) $(DDOC_BLANKLINE ) $(P This unittest verifies that $(D myFunc) contains only $(D @safe), nothrow code. Although this can also be accomplished by attaching these attributes to $(D myFunc) itself, that would prevent $(D myFunc) from being instantiated with types $(D T) that have $(D @system) or throwing code in their $(D opAssign) method, or other methods that $(D myFunc) may call. The above idiom allows $(D myFunc) to be instantiated with such types, yet at the same time verify that the $(D @system) and throwing behavior is not introduced by the code within $(D myFunc) itself.) $(DDOC_BLANKLINE ) $(IMPLEMENTATION_DEFINED $(OL $(LI If unit tests are not enabled, the implementation is not required to check the $(GLINK UnitTest) for syntactic or semantic correctness. This is to reduce the compile time impact of larger unit test sections. The tokens must still be valid, and the implementation can merely count $(D {) and $(D }) tokens to find the end of the $(GLINK UnitTest)'s $(GLINK2 statement, BlockStatement). ) $(LI The presentation of unit test results to the user.) $(LI The method used to enable or disable the unit tests. Use of a compiler switch such as $(DDSUBLINK dmd, switch-unittest, $(B -unittest)) to enable them is suggested.) $(LI The order in which modules are called to run their unit tests.) $(LI Whether the program stops on the first unit test failure, or continues running the unit tests.) ) ) $(DDOC_BLANKLINE ) $(BEST_PRACTICE $(OL $(LI Using unit tests in conjunction with coverage testing (such as $(DDSUBLINK dmd, switch-cov, $(B -cov))) is effective.) $(LI A unit test for a function should appear immediately following it.) ) ) $(DDOC_BLANKLINE ) $(DDOC_BLANKLINE )

$(LNAME2 documented-unittests, Documented Unittests)

$(DDOC_BLANKLINE ) $(P Documented unittests allow the developer to deliver code examples to the user, while at the same time automatically verifying that the examples are valid. This avoids the frequent problem of having outdated documentation for some piece of code.) $(DDOC_BLANKLINE ) $(P If a declaration is followed by a documented unittest, the code in the unittest will be inserted in the $(B example) section of the declaration:) $(DDOC_BLANKLINE ) $(D_CODE $(D_COMMENT /// Math class )$(D_KEYWORD class) Math { $(D_COMMENT /// add function ) $(D_KEYWORD static) $(D_KEYWORD int) add($(D_KEYWORD int) x, $(D_KEYWORD int) y) { $(D_KEYWORD return) x + y; } $(D_COMMENT /// ) $(D_KEYWORD unittest) { $(D_KEYWORD assert)(add(2, 2) == 4); } } $(D_COMMENT /// )$(D_KEYWORD unittest) { $(D_KEYWORD auto) math = $(D_KEYWORD new) Math(); $(D_KEYWORD auto) result = math.add(2, 2); } ) $(DDOC_BLANKLINE ) $(P The above will generate the following documentation:) $(DDOC_BLANKLINE ) $(RAWHTML
class Math;
Math class

Example:
auto math = new Math;
auto result = math.add(2, 2);

int add(int x, int y);
add function

Example:
assert(add(2, 2) == 4);
) $(DDOC_BLANKLINE ) $(P A unittest which is not documented, or is marked as private will not be used to generate code samples.) $(DDOC_BLANKLINE ) $(P There can be multiple documented unittests and they can appear in any order. They will be attached to the last non-unittest declaration:) $(DDOC_BLANKLINE ) $(D_CODE $(D_COMMENT /// add function )$(D_KEYWORD int) add($(D_KEYWORD int) x, $(D_KEYWORD int) y) { $(D_KEYWORD return) x + y; } $(D_COMMENT /// code sample generated )$(D_KEYWORD unittest) { $(D_KEYWORD assert)(add(1, 1) == 2); } $(D_COMMENT /// code sample not generated because the unittest is private )$(D_KEYWORD private) $(D_KEYWORD unittest) { $(D_KEYWORD assert)(add(2, 2) == 4); } $(D_KEYWORD unittest) { $(D_COMMENT /// code sample not generated because the unittest isn't documented ) $(D_KEYWORD assert)(add(3, 3) == 6); } $(D_COMMENT /// code sample generated, even if it only includes comments $(LPAREN)or is empty$(RPAREN ) )$(D_KEYWORD unittest) { $(D_COMMENT /** assert$(LPAREN)add$(LPAREN)4, 4$(RPAREN ) == 8$(RPAREN ); */) } ) $(DDOC_BLANKLINE ) $(P The above will generate the following documentation:) $(DDOC_BLANKLINE ) $(RAWHTML
int add(int x, int y);
add function

Examples:
code sample generated
assert(add(1, 1) == 2);


Examples:
code sample generated, even if it is empty or only includes comments
/** assert(add(4, 4) == 8); */


) $(DDOC_BLANKLINE ) $(DDOC_BLANKLINE ) $(SPEC_SUBNAV_PREV_NEXT errors, Error Handling, garbage, Garbage Collection) ) )