是否可以在没有 运行 编译 unittest 的情况下编译它们,并为特定模块显式 运行 unittest?

Is it possible to compile unittest without running them and explicitly run unittest for a specific module?

在开发API时,我经常在main函数中编写测试代码,但因为D已经集成了unittest,所以我想开始使用它们。

我当前的工作流程如下,我有一个脚本可以监视任何 .d 文件中的文件更改,如果脚本发现修改过的文件,它将 运行 dub build

问题是 dub build 似乎没有构建单元测试

module foo

struct Bar{..}

unittest{
...
// some syntax error here
...
}

如果我明确 运行 dub test,它只会编译单元测试。但是我不想 运行 同时编译它们。

第二个问题是我希望能够 运行 例如对单个模块进行单元测试

dub test module foo

这可能吗?

您可以使用特征getUnittests编写自定义测试运行程序。

getUnitTests

Takes one argument, a symbol of an aggregate (e.g. struct/class/module). The result is a tuple of all the unit test functions of that aggregate. The functions returned are like normal nested static functions, CTFE will work and UDA's will be accessible.

在您的 main() 中,您应该能够编写采用任意数量模块的内容:

void runModuleTests(Modules...)()
{
    static if (Modules.length > 1)
        runModuleTests!(Modules[1..$]);
    else static if (Modules.length == 1)
        foreach(test; __traits(getUnitTests, Modules[0])) test;
}

当然开关-unittest必须传给dmd