unittest "name" {}

bachmeier no at spam.net
Fri Feb 17 03:48:16 UTC 2023


On Thursday, 16 February 2023 at 23:21:11 UTC, Paul Backus wrote:
> On Thursday, 16 February 2023 at 17:26:20 UTC, bachmeier wrote:
>> My opinion is that naming a unit test is the wrong approach. 
>> If the condition for an `enforce` statement fails, it returns 
>> an informative message, not a name attached to the `enforce` 
>> statement. Along those lines, I would prefer this:
>>
>> ```
>> unittest {
>>   onFailure("Comparison of transformations failed");
>>   ...
>> }
>> ```
>
> ```
> unittest
> {
>     import std.stdio;
>
>     scope(failure) writeln("Test failed");
>     assert(1 + 1 == 3);
> }
> ```

That works, but it's verbose, and if you run this program

```
import std.stdio;

unittest {
   assert(3 == 2);
   scope(failure) { writeln("running failure code"); }
}

void main() {}
```

The output is

```
[unittest] Assertion failure
1/1 modules FAILED unittests
```

so it's not exactly the same as an `onFailure` that could go at 
the top or bottom. You can also use version statements if you 
want to selectively run unit tests, so the whole discussion is 
really about convenience.


More information about the Digitalmars-d mailing list