Suggestion (ping Walter): Improve unit testing.

Chris Nicholson-Sauls ibisbasenji at gmail.com
Sat Apr 21 10:03:33 PDT 2007


Ary Manzana wrote:
> I also very much agree.
> 
> If a summary is going to be printed for the unittests (i.e. TESTS: 1 
> PASSED: 0  FAILED: 1), it could be great also to label unittests with a 
> name. This is more informative that just the file and line where the 
> assertion failed. Compare "foo.bar.Exception.AssertException on 
> test.d(7)" to "heap sort makes list sorted".
> 
> To to this, you can optionaly pass a string to the unittest:
> 
> unittest("heap sort makes list sorted") {
>    // ...
> }
> 
> It's backward compatible, and allows future nicer integration with IDEs: 
> they could show a list of the tests passed and failed, and names are 
> mandatory in this case.
> 

Ary, I was just about to suggest that.  And i would also like very much to see these kind 
of improvements made.  (And Gregor, that's darn slick work.)  Reiner Pope and I tried to 
make unittests at least a little more verbal in Cashew's UTest module, but it ends up 
taking over the way you write unittests.  Consider this snip from Cashew's array utils:

--------------------------------------------------
version (Unittest) {
   static import UTest = cashew .utils .UTest ;
}

unittest {
   UTest.Stdout(""c);
   UTest.beginModule("cashew.utils.array");
}

unittest { UTest.beginSection("stand-alone"); }

T[] repeat (T) (T needle, size_t len)
body {
   T[] haystack = new T[len];

   haystack[] = needle;
   return haystack;
}
unittest {
   UTest.begin(r" repeat(T [, N])"c);
   assert(repeat(3, 3U) == [3, 3, 3]);
   UTest.end;
}

unittest {
   UTest.endSection;
   UTest.beginSection("pseudo-member");
}

bool contains (T) (T[] haystack, T needle)
body {
   return haystack.indexOf(needle) != NOT_FOUND;
}
unittest {
   UTest.begin(r".contains(T)"c);
   int[] foo = [1, 2, 3];
   assert(  foo.contains(2));
   assert(! foo.contains(4));
   UTest.end;
}

unittest {
   UTest.endSection;
   UTest.endModule;
}
--------------------------------------------------

Ack.  But the output is nice.

--------------------------------------------------
Unittest: cashew.utils.array: begin
	[stand-alone]
	, 	 array(...)------------------> Pass
	, 	 repeat(T [, N])-------------> Pass
	, 	 repeatSub(T[], N)-----------> Pass
	, 	 assoc([],[])----------------> Pass
	[pseudo-member]
	, 	.defaultLength(N)------------> Pass
	, 	.contains(T)-----------------> Pass
	, 	.diff(T[])-------------------> Pass
	, 	.intersect(A,A)--------------> Pass
	, 	.indexOf(T)------------------> Pass
	, 	.indexOfSub(T[])-------------> Pass
	, 	.rindexOf(T)-----------------> Pass
	, 	.rindexOfSub(T[])------------> Pass
	, 	.remove(T)-------------------> Pass
	, 	.removeAll(T)----------------> Pass
	, 	.drop(N)---------------------> Pass
	, 	.dropIf(Dlg)-----------------> Pass
	, 	.dropRange(N,M)--------------> Pass
	, 	.extract(N)------------------> Pass
	, 	.extractRange(N,M)-----------> Pass
	, 	.shift()---------------------> Pass
	, 	.rshift()--------------------> Pass
	, 	.eat(N)----------------------> Pass
	, 	.reat(N)---------------------> Pass
	, 	.fill (T [, N])--------------> Pass
	, 	.fillSub (T[] [, N])---------> Pass
	, 	.unique()--------------------> Pass
	, 	.rotl(N)---------------------> Pass
	, 	.rotr(N)---------------------> Pass
	, 	.push(...)-------------------> Pass
	, 	.shove(...)------------------> Pass
	, 	.filter(Dlg)-----------------> Pass
	, 	.find(Dlg)-------------------> Pass
	, 	.apply(Dlg)------------------> Pass
	, 	.replace(T,T)----------------> Pass
	, 	.replacePairs(T[T])----------> Pass
	, 	.join(T[][], T[])------------> Pass
	, 	.append(T[], T[]...)---------> Pass
	, 	.split(T[]...)---------------> Pass
	, 	.splitLen(N)-----------------> Pass
	, 	.greedySplitLen(N)-----------> Pass
Unittest: cashew.utils.array: end
--------------------------------------------------

I'd very much like to be able to just toss that thing out some day.  :)  For the morbidly 
curious, here is UTest:
http://dsource.org/projects/cashew/browser/trunk/cashew/utils/UTest.d

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list