How can one reliably run unittests

jmh530 john.michael.hall at gmail.com
Thu Aug 26 00:20:38 UTC 2021


On Wednesday, 25 August 2021 at 22:44:40 UTC, Walter Bright wrote:
> On 8/25/2021 2:05 PM, Paul Backus wrote:
>> The problem with -main is that it's too blunt an instrument: 
>> it will add a main function whether the code you're compiling 
>> already has one or not. That means you have to keep track of 
>> which modules have main functions and which ones don't, so 
>> that you (or more realistically, your build system) can add 
>> -main only when it's necessary.
>
> That problem has never occurred to me, and has never happened 
> to me, yet I use -main all the time.
>
> One way to resolve it is just put your main() in a separate 
> module.

What if you are dealing with something that is a single file?

If you run below on run.dlang.org with either `-unittest` or 
`-unittest -main`, then you get a message that only one main is 
allowed. If you remove the dependency, then it compiles without 
error with `-unittest` but gets the same error with `-unittest 
-main`. The difference is (IIRC) when you include a dependency it 
is also calling `-single`. If you had something like 
`-main=ifmissing` that others describe, then there would be a 
consistent solution that could work well with run.dlang.org.

```d
/+dub.sdl:
dependency "mir-algorithm" versio="*"
+/

unittest {}
void main() {}
```


More information about the Digitalmars-d mailing list