July 22, 2008
Hi,

I'm in the middle of building a mid-size application in D and have been conscientious about building unittests in all of my classes.

The main() module as well as all component modules have been built with -funittest.   I know that unittests are run before main() starts, but my problem is that gdc is only running the unittests that appear in the main() module and not those that are linked in from supporting object modules.

Is there a way to enable the unittests that are linked into main()?

The crazy thing is that my main module contains no class definitions so no unittests are actually being run.  I verified  this in gdc by creating a small class definiton with built-in unittest.

I can, of course, cruft up a dummy "void main()" in each of the component modules, but that's pretty ugly.

(BTW: I've seen some of the utilities that run all unittests, but I'm hoping there's a better solution.)

Any help greatly appreciated.

Thanks,

Jim Burnes
Boulder, CO

July 22, 2008
Jim Burnes wrote:

> Hi,
> 
> I'm in the middle of building a mid-size application in D and have been conscientious about building unittests in all of my classes.
> 
> The main() module as well as all component modules have been built with
> -funittest.   I know that unittests are run before main() starts, but my
> problem is that gdc is only running the unittests that appear in the
> main() module and not those that are linked in from supporting object
> modules.
> 
> Is there a way to enable the unittests that are linked into main()?
> 
> The crazy thing is that my main module contains no class definitions so no unittests are actually being run.  I verified  this in gdc by creating a small class definiton with built-in unittest.
> 
> I can, of course, cruft up a dummy "void main()" in each of the component modules, but that's pretty ugly.
> 
> (BTW: I've seen some of the utilities that run all unittests, but I'm hoping there's a better solution.)
> 
> Any help greatly appreciated.
> 
> Thanks,
> 
> Jim Burnes
> Boulder, CO

If they aren't run it is a bug somewhere. FWIW, the Tango runtime have a hook to give you more control of the unittests you run. The following is used to run Tango unittests (all files are built and linked in via rebuild).

import tango.io.Stdout;
import tango.core.Runtime;

bool tangoUnitTester()
{
    uint countFailed = 0;
    uint countTotal = 1;
    Stdout ("NOTE: This is still fairly rudimentary, and will only report
the").newline;
    Stdout ("    first error per module.").newline;
    foreach ( m; ModuleInfo )  // _moduleinfo_array )
    {
        if ( m.unitTest) {
            Stdout.format ("{}. Executing unittests in '{}' ", countTotal,
m.name);
            countTotal++;
            try {
               m.unitTest();
            }
            catch (Exception e) {
                countFailed++;
                Stdout(" - Unittest failed.").newline;
                Stdout.format("   File '{}', line '{}'.", e.file,
e.line).newline;
                Stdout.format("     Message is : '{}'", e.msg).newline;
                if (e.info)
                    Stdout.format("     TraceInfo: {}",
e.info.toString).newline;
                continue;
            }
            Stdout(" - Success.").newline;
        }
    }

    Stdout.format ("{} out of {} tests failed.", countFailed, countTotal -
1).newline;
    return true;
}

static this() {
    Runtime.moduleUnitTester( &tangoUnitTester );
}

void main() {}


-- 
Lars Ivar Igesund
blog at http://larsivi.net
DSource, #d.tango & #D: larsivi
Dancing the Tango