Named unittests

Johannes Loher johannes.loher at fg4f.de
Sun May 19 06:36:56 UTC 2019


Am 19.05.19 um 07:59 schrieb Petar Kirov [ZombineDev]:
[...]
> 
> If the dependencies are templated, like large portions of Phobos, then
> no, they and their unit tests will be compiled together with your code.
> 
> When you import a module, the compiler adds its unit tests to the list
> of all unit tests that need to be run. At the moment the compiler can't
> know if you're importing your own module (that you want to test) or a
> third-party one (that you don't care about its unit tests).

This is not entirely true. Only unittest blocks that are included in the
modules that are actually compiled are run. There is one nasty corner
case however. Consider the following excample:

--- main.d

import foo;
import std.stdio;

void main()
{
    writeln(add(1, 1));
}

unittest
{
    writeln("unittest in main");
    assert(add(1, 2) == 3);
}

--- foo.d

template add(T)
{
    T add(T a, T b)
    {
        return a + b;
    }

    unittest
    {
        import std.stdio : writeln;

        writeln("unittest in foo.add");
        assert(add(1, 2) == 3);
    }

}

unittest
{
    import std.stdio : writeln;

    writeln("unittest in foo");
    assert(add(1, 2) == 3);
}

When compiling and running with `dmd -unittest -run main.d`, this prints

unittest in main
unittest in foo.add
2

As you can see, the unittest block located in the template is executed,
while the one at module level in foo is not. This is because templates
are expanded at call site, so from the compilers perspective it looks
like the unittest was inside main.d, which is being compiled, so the
unittest is also compiled (for this particular instantiation of add).

There have been ideas to fix this by moving the unittests outside the
template before expanding it by modifying the AST or something like that
(I believe I hear Jonathan talk about that during DConf) but nobody has
gotten around tom implementing that yet and it also sound a bit hackish
to me...


More information about the Digitalmars-d mailing list