[phobos] Silent failure of std.container unittests

Michel Fortin michel.fortin at michelf.com
Fri Jul 16 19:37:39 PDT 2010


Le 2010-07-16 à 21:03, Jonathan M Davis a écrit :

>> My thought is, that we might be running into issues because the idea of
>> a unit test is being conflated with a function. What we really want is a
>> collection of blocks of code that that are all run and if any of them
>> trigger an assert, that block stops and the tests as a whole are
>> considered to have failed.
>> 
>> As a point to start from, how about allow unittest as a statement type?
>> 
>> foreach(item; collection) unittest { asssert(foobar(item)); }
> 
> Regardless of whether you consider the assertion or the unittest block as the 
> unit test (and I would certainly choose the latter), I don't see any point to 
> this.

Walter had a point... what if you want to test multiple outputs of the same function on one go? Here's an example:

	unittest {
		int[3] result = makeSomeTable(399, 383, 927);
		assert(result[0] == 281);
		assert(result[1] == 281);
		assert(result[2] == 281);
	}

Here, each assert is independent one from another and knowing that the first and the third fails but not the second might help diagnose the problem.

I think Jonathan's idea is quite on the spot. It'd allow you to write this:

	unittest {
		int[3] result = makeSomeTable(399, 383, 927);
		unittest { assert(result[0] == 281); }
		unittest { assert(result[1] == 281); }
		unittest { assert(result[2] == 281); }
	}

and each "unittest assert" becomes an independent test that may fail but without interrupting the flow of the outside scope. The compiler could expand it like this:

	unittest {
		int[3] result = makeSomeTable(399, 383, 927);

		try
			assert(result[0] == 281);
		catch (AssertionError e)
			__unitTestLogFailure(e);

		try
			assert(result[1] == 281);
		catch (AssertionError e)
			__unitTestLogFailure(e);

		try
			assert(result[2] == 281);
		catch (AssertionError e)
			__unitTestLogFailure(e);
	}

And now you have a log entry for each failed assertion, because they're independent unit tests.

It'd also help reduce verbosity to not require the braces for one-statement unit tests.


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/





More information about the phobos mailing list