unit tests with name and verbose report

jfondren julian.fondren at gmail.com
Thu Aug 4 20:18:08 UTC 2022


On Thursday, 4 August 2022 at 17:08:39 UTC, Danesh Daroui wrote:
> Hi all,
>
> These questions can be redundant. I searched in the forum but 
> didn't find any final conclusion so I am asking them here.
>
> I have two questions:
>
> 1. Would it be possible to have "named" unit tests? Right now 
> only anonymous unit tests ara apparently supported in D and 
> when the tests are executed no detailed information is shown. I 
> would like to see how many tests have been executed, how many 
> passed and how many failed and complete names of the test for 
> both passed and failed.

Here's a trivial, complete script:

```d
#! /usr/bin/env dub
/++ dub.sdl:
     dflags "-preview=shortenedMethods"
     configuration "release" {
         targetType "executable"
     }
     configuration "unittest" {
         targetType "library"
         dependency "silly" version="~>1.1.1"
     }
+/

int factorial(int n) => n <= 1 ? 1 : n * factorial(n - 1);

@("!5") unittest {
     assert(factorial(5) == 120);
}

@("!0 and !1") unittest {
     assert(factorial(0) == 1);
     assert(factorial(1) == 1);
}

version (unittest) {
} else {
     void main(string[] args) {
         import std.conv : to;
         import std.stdio : writeln;

         writeln(args[1].to!int.factorial);
     }
}
```

Usage:

```d
$ ./fact.d 10
3628800
$ dub -q test --single fact.d
  ✓ fact !5
  ✓ fact !0 and !1

Summary: 2 passed, 0 failed in 0 ms
```

More elaborate unit testing with custom test runners is very nice 
in D actually, but it's slightly more work to set up.

> 2. Does D support (natively) AI and machine learning and 
> reasoning techniques? I mean something like backtracking, 
> triple stores, managing a knowledge base, etc.?

I'd start looking for that here: 
https://code.dlang.org/packages/mir



More information about the Digitalmars-d mailing list