unit tests with name and verbose report

Danesh Daroui danesh.daroui at gmail.com
Fri Aug 5 12:16:25 UTC 2022


On Thursday, 4 August 2022 at 20:18:08 UTC, jfondren wrote:
> 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

Thank you all for your answers.

The unittest with version(unittest) didn't work for me and I got 
link error from the compiler since it didn't find the main() 
function.

Frankly, I didn't like the way to use a macro (version(X) is a 
macro, right?) to use such a simple feature. I think such report 
to indicate which test is running and either succeeded or failed 
is an essential feature and I am surprised how it is not included 
in the compiler yet. To me, anonymous unittests are rather "show 
off" of a language! :)

The "mir" is a good one but I was looking for machine reasoning 
algorithms which you can implement an expert system based on 
rules and facts in a knowledge base. It seems that it is not 
implemented. Thanks. :)

Regards,

Dan



More information about the Digitalmars-d mailing list