Enabling Only Top-Level Unittests

Basile B via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 23 07:55:16 PDT 2016


On Friday, 22 April 2016 at 18:18:39 UTC, Basile B. wrote:
> I think a DMD option should be added to allow the tests to be 
> compiled but never called, something like -runTests. Because 
> the first solution is much more attractive.

Actually the first solution works with:

https://dlang.org/phobos/core_runtime.html#.Runtime.moduleUnitTester

---
#!runnable-flags: -unittest -main
module m;

import std.stdio;
import core.runtime;

enum USERTEST;

@USERTEST unittest // exeuted once
{
     writeln("bang");
}

unittest
{
     writeln("bing");
}

bool tester()
{
     import std.traits;
     foreach(t; __traits(getUnitTests, m))
     {
         static if (hasUDA!(t, USERTEST))
             t();
     }
     return true;
}

static this()
{
     Runtime.moduleUnitTester = &tester;
}
---

So there's really no problem with selecting the top-level UT. 
Maybe it even works with other traits such as "parent". Or with a 
template that collects only the tests in a set of module like in 
the previous attempt (based on this: 
http://stackoverflow.com/questions/35075253/is-it-possible-to-compile-unittest-without-running-them-and-explicitly-run-unitt/35086752#35086752).


More information about the Digitalmars-d-learn mailing list