Thread overview
DMD D2: Unittest in static lib
Aug 04, 2010
Ettienne Gilbert
Aug 04, 2010
Don
Aug 04, 2010
Jesse Phillips
August 04, 2010
Hi,

I am experienced in C++ but still a relative D newbie...

I am building a static lib containing a bunch of functions that I often re-use. I will use these in other exe/dll projects by linking to the lib [and specifying the necessary Import paths..].

Everything is working ok when I test the concept, but the problem is I cannot get any unit-tests to run in the lib [even though the compiler "-unittest" option is passed to DMD compiler and it compiles fine].

I guess the problem is the lib does not have a main(..) function and is not meant to run as "a standalone". I've therefore created a separate Console application [and linked to the debug version of the lib], in an attempt to trigger the unit-tests in the lib - but to no avail.

I can of course do the actual unit-tests inside this Console test application, but that kinda defeats the purpose of keeping the unit-tests with the actual code! I would really prefer to keep the unit-tests in the modules of the lib, and not the test-app module.

I have even  - in desperation [did not really think it would work] - tried to pass "--main" as an argument to the compiler [described by Andrei in "The D Programming Language" in $5.1 on p133], in the hope that I can fool the compiler to create a exe, rather than a lib for the debug version of the lib. But DMD2.047 does seem to like this compiler switch!

How does one get unit-tests that are inside modules in a lib to run?

Thanks,
Ettienne

August 04, 2010
Ettienne Gilbert wrote:
> Hi,
> 
> I am experienced in C++ but still a relative D newbie...
> 
> I am building a static lib containing a bunch of functions that I often
> re-use. I will use these in other exe/dll projects by linking to the lib [and
> specifying the necessary Import paths..].
> 
> Everything is working ok when I test the concept, but the problem is I cannot
> get any unit-tests to run in the lib [even though the compiler "-unittest"
> option is passed to DMD compiler and it compiles fine].
> 
> I guess the problem is the lib does not have a main(..) function and is not
> meant to run as "a standalone". I've therefore created a separate Console
> application [and linked to the debug version of the lib], in an attempt to
> trigger the unit-tests in the lib - but to no avail.
> 
> I can of course do the actual unit-tests inside this Console test application,
> but that kinda defeats the purpose of keeping the unit-tests with the actual
> code! I would really prefer to keep the unit-tests in the modules of the lib,
> and not the test-app module.
> 
> I have even  - in desperation [did not really think it would work] - tried to
> pass "--main" as an argument to the compiler [described by Andrei in "The D
> Programming Language" in $5.1 on p133], in the hope that I can fool the
> compiler to create a exe, rather than a lib for the debug version of the lib.
> But DMD2.047 does seem to like this compiler switch!
> 
> How does one get unit-tests that are inside modules in a lib to run?
> 
> Thanks,
> Ettienne
> 

AFAIK you need to create a module which is just void main(){} which imports all the modules in your lib. See dmd/src/phobos/unittest.d for how it's done in Phobos.
August 04, 2010
On Wed, 04 Aug 2010 12:50:45 +0000, Ettienne Gilbert wrote:

> I have even  - in desperation [did not really think it would work] - tried to pass "--main" as an argument to the compiler [described by Andrei in "The D Programming Language" in $5.1 on p133], in the hope that I can fool the compiler to create a exe, rather than a lib for the debug version of the lib. But DMD2.047 does seem to like this compiler switch!
> 
> How does one get unit-tests that are inside modules in a lib to run?
> 
> Thanks,
> Ettienne

Unit tests are run prior to the execution of main. So you must run the program for the Unittests to run. Also  --main is a switch for rdmd and not dmd.