Runtime.moduleUnitTester

Overrides the default module unit tester with a user-supplied version. This routine will be called once on program initialization. The return value of this routine indicates to the runtime whether the tests ran without error.

There are two options for handlers. The bool version is deprecated but will be kept for legacy support. Returning true from the handler is equivalent to returning UnitTestResult.pass from the extended version. Returning false from the handler is equivalent to returning UnitTestResult.fail from the extended version.

See the documentation for UnitTestResult to see how you should set up the return structure.

See the documentation for runModuleUnitTests for how the default algorithm works, or read the example below.

  1. void moduleUnitTester(ModuleUnitTester h)
    struct Runtime
    static @property
    void
    moduleUnitTester
  2. ModuleUnitTester moduleUnitTester()
  3. void extendedModuleUnitTester(ExtendedModuleUnitTester h)

Parameters

h

The new unit tester. Set both to null to use the default unit tester.

Examples

1 shared static this()
2 {
3    import core.runtime;
4 
5    Runtime.extendedModuleUnitTester = &customModuleUnitTester;
6 }
7 
8 UnitTestResult customModuleUnitTester()
9 {
10    import std.stdio;
11 
12    writeln("Using customModuleUnitTester");
13 
14    // Do the same thing as the default moduleUnitTester:
15    UnitTestResult result;
16    foreach (m; ModuleInfo)
17    {
18        if (m)
19        {
20            auto fp = m.unitTest;
21 
22            if (fp)
23            {
24                ++result.executed;
25                try
26                {
27                    fp();
28                    ++result.passed;
29                }
30                catch (Throwable e)
31                {
32                    writeln(e);
33                }
34            }
35        }
36    }
37    if (result.executed != result.passed)
38    {
39        result.runMain = false;  // don't run main
40        result.summarize = true; // print failure
41    }
42    else
43    {
44        result.runMain = true;    // all UT passed
45        result.summarize = false; // be quiet about it.
46    }
47    return result;
48 }
Suggestion Box / Bug Report