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

[亡魂溺海] 提交于 2019-12-22 08:35:53

问题


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.

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

The problem is that dub build doesn't seem to build the unittest

module foo

struct Bar{..}

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

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 test module foo

Would this be possible?


回答1:


You can program a custom test runner using the trait 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.

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;
}

of course the switch -unittest must be passed to dmd



来源:https://stackoverflow.com/questions/35075253/is-it-possible-to-compile-unittest-without-running-them-and-explicitly-run-unitt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!