How can one reliably run unittests

wjoe invalid at example.com
Wed Aug 25 13:23:22 UTC 2021


On Wednesday, 25 August 2021 at 11:46:18 UTC, jfondren wrote:
> 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

But due to the fact that all unittests are compiled into one 
callable, i.e. it's all in the same process, doesn't that mean 
that after catching a throwable that the program is in UB ? And 
isn't that the reason why the runtime unittester aborts on the 
1st failure ?
So shouldn't it be like every module has its own callable which 
can then run in separate processes which then wouldn't affect 
others if one of them failed ? Also, that way all the modules 
could be tested in parallel.


More information about the Digitalmars-d mailing list