How can one reliably run unittests

Paul Backus snarwin at gmail.com
Wed Aug 25 21:05:42 UTC 2021


On Wednesday, 25 August 2021 at 20:49:32 UTC, Walter Bright wrote:
> On 8/24/2021 5:21 AM, deadalnix wrote:
>> Adding a main for libraries is required, or it won't link, but 
>> adding one to executable is going to cause a link error.
>
> This is indeed a real problem. It was solved with the -main 
> switch to dmd, which will just stick one in for you. I use it 
> all the time for unit testing modules.

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.

If we had something like -main=ifMissing that added an empty main 
function only if the modules being compiled didn't already have 
one, then it would be possible to unit-test any D module with a 
command line like

     dmd -i -unittest -main=ifMissing -run mymodule.d

Being able to use the same command for everything makes 
automation much easier--you can write it in a makefile rule, put 
it in a script, etc.


More information about the Digitalmars-d mailing list