Why can't we put import in functions.

Bill Baxter dnewsgroup at billbaxter.com
Tue Oct 16 11:11:28 PDT 2007


Bruce Adams wrote:
> Henning Hasemann Wrote:
> 
>> I came to (nearly) entirely drop unittests and instead make "module
>> tests" where I compile each module into a binary and run it.
>>
>> It goes like this:
>>
>> module foo;
>> import bar;
>>
>> // Class defitions etc...
>>
>> version(module_test_foo) {
>>   import test_dep;
>>   void main() {
>>     // test code
>>   }
>> }
>>
>> I know it isnt particular prettier either, especially as you have to be
>> careful about reusing your object files and have a lot of extra
>> compiling to do for the tests. But on the other hand the modules
>> included can "see" which module is beeing tested and file some special
>> cases for it to test or whatever.
>>
>> I also use an additional -version=module_test which in some places is
>> needed (in my code) to skip some initialization functions that
>> otherwisely might "bite" with the main.
>>
>> Also note that this is for library code where there would be no main()
>> at all, so it wouldnt be testable without having an extra main.
>>
>> Henning
>>
>> -- 
>> GPG Public Key:
>> http://gpg-keyserver.de/pks/lookup?op=get&search=0xDDD6D36D41911851
> 
> I tend to follow a cppunit like model. For every class I create a test class which has a main. This gets all your unittest stuff into a separate file from the implementation so it doens't get too big. 

Putting unittests in a separate file also has the advantage of enabling 
you to detect errors in access attributes (As in "oops! That wasn't 
supposed to be private!").


--bb



More information about the Digitalmars-d mailing list