Enabling Only Top-Level Unittests

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 22 11:18:39 PDT 2016


On Thursday, 21 April 2016 at 19:38:21 UTC, Nordlöw wrote:
> On Thursday, 21 April 2016 at 12:35:42 UTC, Anonymouse wrote:
>> Then dmd -unittest -version=TestDeps if you want them run.
>
> This doesn't make things easier. I want to disable the builtin 
> unittests of the modules I've imported. This requires me to add 
> a
>
> version(test_MODULE) unittest
>
> in each module named MODULE.
>
> This is really inconvenient.

You could anotate your user/top level tests and select only those 
with __traits(getUnittest,...) and hasUDA(). Unfortunately it 
seems that there's a flaw because in a program all the tests will 
be automatically executed once then again those that are anotated.

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

import std.stdio;

enum USERTEST;

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

unittest // executed once
{
     writeln("bing");
}

void main(string[] args)
{
     import std.traits;
     foreach(t; __traits(getUnitTests, m))
     {
         static if (hasUDA!(t, USERTEST))
             t();
     }
}
---

But...
A solution that would work for both a library and a program is 
this:

---
module m;

import std.stdio;

enum USERTEST;

@USERTEST void test0()
{
     writeln("bing");
}

@USERTEST void test1()
{
     writeln("bang");
}

unittest
{
     // not even compiled
}

void runUserTests(Modules...)()
{
     import std.traits;
     static if (Modules.length > 1)
         runModuleTests!(Modules[1..$]);
     else static if (Modules.length == 1)
     {
         foreach(member; __traits(allMembers, Modules[0]))
             foreach(o; __traits(getOverloads, Modules[0], member))
                 static if (hasUDA!(o, USERTEST) && 
(Parameters!o).length == 0)
                     o();
     }
}

void main()
{
     runUserTests!m;
}
---

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.


More information about the Digitalmars-d-learn mailing list