run unittest inside program

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 5 21:15:39 PDT 2017


On Wednesday, September 06, 2017 02:06:59 Psychological Cleanup via 
Digitalmars-d-learn wrote:
> Is it possible to run a unit test without adding -unittest to the
> command line?
>
> unittest X
> {
>     pragma(msg, "Boo!");
> }
>
> X;
>
> void main() { }

No. Without -unittest, the unittest blocks and anything that's
version(unittest) aren't compiled in. If you're worried about running main
after the unit tests (since unfortunately, the way that -unittest works is
that it compiles in your normal main to run after the unit tests), then you
can do something like

version(unittest) void main() {}
else void main()
{
    ...
}

And you can always put tests in functions rather than unittest blocks if you
want to call them normally.

There are also some unit test frameworks on code.dlang.org which allow you
to mess with how unit tests work in various ways, but fundamentally,
-unittest must be used in order to have unittest blocks and
version(unittest) blocks compiled in.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list