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

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

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

问题是配音构建似乎没有构建单元测试

module foo

struct Bar{..}

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

如果我明确地运行dub测试,它只编译单元测试.但我不想同时运行和编译它们.

第二个问题是我希望能够为单个模块运行单元测试

配音测试模块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;
}

当然必须将switch -unittest传递给dmd

点赞