How can one reliably run unittests

jfondren julian.fondren at gmail.com
Wed Aug 25 11:46:18 UTC 2021


On Wednesday, 25 August 2021 at 11:11:24 UTC, Steven 
Schveighoffer wrote:
> The only thing that isn't provided is running individual tests, 
> but that is a compiler issue (the compiler combines all 
> unittests in a module into one callable).

You can run individual tests:

```d
module tester1;

unittest { assert(true); }
unittest { assert(!!true); }
unittest { assert(1 != 1); }
unittest { assert(1 > 0); }

version (unittest) {
     bool tester() {
         import std.meta : AliasSeq;
         import std.stdio : writef, writeln;

         alias tests = AliasSeq!(__traits(getUnitTests, tester1));
         static foreach (i; 0 .. tests.length) {
             writef!"Test %d/%d ..."(i + 1, tests.length);
             try {
                 tests[i]();
                 writeln("ok");
             } catch (Throwable t) {
                 writeln("failed");
             }
         }
         return false;
     }

     shared static this() {
         import core.runtime : Runtime;

         Runtime.moduleUnitTester = &tester;
     }
}

void main() {
     assert(false); // this doesn't get run
}
```

usage:

```
$ dmd -unittest -run tester1.d
Test 1/4 ...ok
Test 2/4 ...ok
Test 3/4 ...failed
Test 4/4 ...ok
```

I found this out in 
https://forum.dlang.org/post/bukhjtbxouadyunqwdih@forum.dlang.org 
, which has a tester that calls tests differently depending on 
their having a UDA


More information about the Digitalmars-d mailing list