www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - run unittest inside program

reply Psychological Cleanup <Help Saving.World> writes:
Is it possible to run a unit test without adding -unittest to the 
command line?

unittest X
{
    pragma(msg, "Boo!");
}

X;

void main() { }
Sep 05 2017
parent Jonathan M Davis via Digitalmars-d-learn writes:
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
Sep 05 2017