是否可以在不运行单元测试的情况下编译unittest并为特定模块显式运行unittest? [英] Is it possible to compile unittest without running them and explicitly run unittest for a specific module?
问题描述
我在开发API时经常在主函数中编写测试代码,但由于D已集成单元测试,所以我想开始使用它们。
I often wrote my test code in the main function while developing an API but because D has integrated unittest I want to start using them.
我当前的工作流程是在下面的代码中,我有一个脚本来监视任何.d文件中的文件更改,如果脚本找到修改后的文件,它将运行 dub build
My current work flow is the following, I have a script that watches for file changes in any .d files, if the scripts finds a modified file it will run dub build
问题在于 dub构建
似乎没有构建单元测试
The problem is that dub build
doesn't seem to build the unittest
module foo
struct Bar{..}
unittest{
...
// some syntax error here
...
}
如果我明确运行 dub测试
。但是我不想同时运行和编译它们。
It only compiles the unittests if I explicitly run dub test
. But I don't want to run and compile them at the same time.
第二个问题是我希望能够运行单个模块的单元测试,例如
The second problem is that I want to be able to run unittests for a single module for example
dub测试模块foo
这可能吗?
推荐答案
您可以使用特征 getUnittests对自定义测试运行程序进行编程。
getUnitTests
getUnitTests
采用一个参数,即集合的符号(例如struct / class /模块)。结果是该集合的所有单元测试功能的元组。返回的函数就像普通的嵌套静态函数一样,CTFE将起作用并且可以访问UDA。
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()
,您应该可以编写使用任意数量模块的东西:
in your main()
you should be able to write something that takes an arbitrary number of module:
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
of course the switch -unittest
must be passed to dmd
这篇关于是否可以在不运行单元测试的情况下编译unittest并为特定模块显式运行unittest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!