selectively running unittest functions

simendsjo simendsjo at gmail.com
Fri Oct 25 07:43:47 PDT 2013


On Friday, 25 October 2013 at 14:14:39 UTC, Dicebot wrote:
> This will work starting with 2.064:
>
> module a;
>
> import std.stdio;
>
> struct ID
> {
>     string data;
> }
>
> @ID("one")
> unittest
> {
>     writeln("1");
> }
>
> @ID("two")
> unittest
> {
>     writeln("2");
> }
>
> void main()
> {
>     import std.typetuple;
>     alias tests = TypeTuple!( __traits(getUnitTests, a) );
>
>     foreach (test; tests)
>     {
>         foreach (uda; __traits(getAttributes, test))
>         {
>             static if (is(typeof(uda) == ID))
>             {
>                 if (uda == ID("two"))
>                     test();
>             }
>         }
>     }
> }
>
> It will print stuff twice though because I didn't suppress 
> native unittest runner. Didn't actually dwell deep into runtime 
> hooks so don't know what is best way to do it (maybe we need 
> one more enhancement for it)

This is great. I love the unittest blocks and UDAs. This way I 
can still have very easy-entry (as in "just write unittest {}") 
unittests while gaining the power of unittest frameworks.

It's a hell of a lot better than having some class with 
attributes containing methods with attributes, containing 
parameters with attributes :) I'm developing RSI when writing 
tests in other languages. (In Java I develop RSI just declaring 
variables...)


More information about the Digitalmars-d-learn mailing list