dmd -unittest works poorly with executables

Neia Neutuladh neia at ikeran.org
Sat Dec 8 23:58:28 UTC 2018


On Sat, 08 Dec 2018 20:16:09 +0000, Andrew Pennebaker wrote:
> I think it's lame to have to use magical code like `version(unittest) {}
> else` to guard our main functions, when we run unit tests. Could D go
> ahead and do the right thing, automatically shadowing our main functions
> when the unit tests are run?

The original intent was for unittests to run before main(). Your debug / 
test builds always run the unittests. This is awkward because unittests 
might sometimes actually be heavier tests that talk to a database or write 
to local files or change global state that would be used in main().

The normal technique with dub is to put *only* main() in app.d. Sometimes 
I take that a step further: I put a runMain() function somewhere in my 
project, and app.d's main() just calls that. Dub automatically excludes 
app.d from the unittest build. Minimum possible code outside the tested 
zone.

Not necessarily awesome, but we do have workarounds.


More information about the Digitalmars-d-learn mailing list