www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Unit testing

reply Sjoerd van Leent <svanleent wanadoo.nl> writes:
Hi all,

As I was doing a presentation on school about unit testing I bumped into 
the following matter:

If I need to describe a unit test testing border values I had to write 
something like the following:

----- CODE -----

class SomeClass {...}

unittest {
	const static short[] testdata = [-1, 0, 1, 99, 100, 101];
	foreach(short data; testdata) {
		try {
			SomeClass some = new SomeClass();
			some.setMethod(data);
		} catch(Exception e) {
			writefln("Value %s caused exception: %s",
                         	  data, e.toString());
		}
	}
}

----- END CODE -----

Can't this be replaced by something like:

----- CODE -----

class SomeClass {...}

unittest around(short data) in [-1, 0, 1, 99, 100, 101] {
	SomeClass some = new SomeClass();
	some.setMethod(data);
}

----- END CODE -----

The keyword around enforces the unittest to be called six times (the 
length of the array) and has a value type short in it. This way, even 
when the setMethod function fails (which is the whole idea of border 
values) it simply goes on to the next value.

Also it makes code much shorter. For unit tests this is definitly a pro.

Perhaps an idea for D 2.0 or some test framework?

Regards,
Sjoerd
Oct 13 2004
parent Sjoerd van Leent <svanleent wanadoo.nl> writes:
Sjoerd van Leent wrote:
 Hi all,
 
 As I was doing a presentation on school about unit testing I bumped into 
 the following matter:
 
 If I need to describe a unit test testing border values I had to write 
 something like the following:
 
 ----- CODE -----
 
 class SomeClass {...}
 
 unittest {
     const static short[] testdata = [-1, 0, 1, 99, 100, 101];
     foreach(short data; testdata) {
         try {
             SomeClass some = new SomeClass();
             some.setMethod(data);
         } catch(Exception e) {
             writefln("Value %s caused exception: %s",
                               data, e.toString());
         }
     }
 }
 
 ----- END CODE -----
 
 Can't this be replaced by something like:
 
 ----- CODE -----
 
 class SomeClass {...}
 
 unittest around(short data) in [-1, 0, 1, 99, 100, 101] {
     SomeClass some = new SomeClass();
     some.setMethod(data);
 }
 
 ----- END CODE -----
 
 The keyword around enforces the unittest to be called six times (the 
 length of the array) and has a value type short in it. This way, even 
 when the setMethod function fails (which is the whole idea of border 
 values) it simply goes on to the next value.
 
 Also it makes code much shorter. For unit tests this is definitly a pro.
 
 Perhaps an idea for D 2.0 or some test framework?
 
 Regards,
 Sjoerd
A better rephrase of my above mentioned code could also be: ----- CODE ----- class SomeClass {...} unittest foreach(short data; [-1, 0, 1, 99, 100, 101]) fail { writefln("Failure on: %s", data); } body { SomeClass some = new SomeClass(); some.setMethod(data); } ----- END CODE ----- This gives the possibility to register the unittest (in this case using writefln) and it is more conform to how D looks. Regards, Sjoerd
Oct 14 2004