unittest "name" {}

Dennis dkorpel at gmail.com
Fri Feb 10 21:08:38 UTC 2023


Names on unittests can be useful for documentation, a better 
message on failure, or to selectively run a specific test. D has 
no built-in way to name unittests, but the most popular library 
test runner ([silly](https://code.dlang.org/packages/silly)) uses 
the first string UDA as a name:

```D
@("Johny")
unittest {
	// This unittest is named Johny
}
```

This isn't too pretty though, and typing parentheses and quotes 
is annoying (using a QWERTY keyboard with US-layout).

I was thinking it could be improved by either allowing 
`@"string"` attributes, similar to `@identifier`, or by allowing 
a string literal after the `unittest` keyword:

```
unittest "Johny" {
     // Has @("Johny") as first UDA
}
```

This would provide a more ergonomic, standardized way of naming 
tests, and it's a very simple parser change (there's currently 
nothing allowed between `unittest {`).

In many other languages, unittests are regular functions with 
names, but 
[Zig-test](https://ziglang.org/documentation/master/#Zig-Test) 
has this syntax for comparison:

```Zig
test "Johny" {
}
```

Question to the community: do you name tests, and do you like the 
idea?



More information about the Digitalmars-d mailing list