Simple bolt-on unittest improvement

Justin Johansson procode at adam-dott-com.au
Fri Sep 11 14:02:18 PDT 2009


Hope it doesn't sound like I just discovered America but being a D newbie one is keen to play with all the language features.  So soon getting round to playing with D's unit test facility with JUnit (Java test unit) experience in mind,  found that I wanted all my tests to run even if one or the tests would otherwise fail on some assert.  Googled for DUnit as a hunch such beast might exist and sure enough found DUnit for D somewhere after the Delphi hits.

http://www.dsource.org/projects/dmocks/wiki/DUnit

Sure enough DUnit writeup spoke about need for continuous testing in D as had occurred to me.  Then it dawned upon me that there really wasn't any need to go away from D's built-in unit test facility to achieve this and hence no need really for the DUnit approach.

The idea is to keep working within D's built in unit test facility; just don't use assert statements in current fashion as if one fails then whole test stops running.  Simply replace assert statements with a function call that tests and records the assert condition instead .. much like in JUnit where you have functions like assertTrue, assertEquals etc.  Then just inside your main program you call up your unit test pretty print report and decide then (say based upon number of failed tests relative to total number of tests) whether to continue with your mainline code or bail out.

The regime now looks something like this:

Class1:
	unittest {
		jjunit.expectEquals( __FILE__, __LINE__, some_x, some_y);
		jjunit.expectTrue( __FILE__, __LINE__, somecond);
	}


Class2:
	unittest {
		jjunit.expectGreaterThan( __FILE__, __LINE__, some_a, some_b);
		jjunit.expectFalse( __FILE__, __LINE__, some_c);
	}

App:
void main()
{
	// print unittest success/fail report and exit if more than 2 failed
	debug if ( dunit.report() > 2) exit( 1)

	// continue with application
	// ...
}


I can't imagine that my approach is anything new.  What do other people do to achieve similar continuous unit testing goal?

Cheers
Justin Johansson




More information about the Digitalmars-d mailing list