Improving unit tests

Christopher Wright dhasenan at gmail.com
Sun Nov 9 06:49:29 PST 2008


Don wrote:
> Sean Kelly wrote:
>> Christopher Wright wrote:
>>> 3. Each test should be its own function.
>>> Currently, all unittest blocks in any module are conglomerated into 
>>> one function, ModuleInfo.unitTest. ModuleInfo should have a list of 
>>> name/function pairs. This is required for named unittests, and for 
>>> reporting errors on a per-test basis.
>>
>> This would be nice as well.  Recovering from unit tests currently 
>> skips all tests in a module after the test that failed, which isn't 
>> terribly desirable.
> 
> Couldn't there be some hidden global bool, isInUnitTest, which is set 
> when the unit tests start to run, and which is set back to false when 
> the last one ends. assert() could have different behaviour depending on 
> whether it's in the unit testing phase, or not.

druntime has pluggable unit test runners, so that's entirely possible. 
You couldn't call that function assert, though.

On the other hand, you might find crashing in these situations if assert 
doesn't abort the current test:

unittest
{
	int[] array = getArray();
	assert (array.length == 1);
	assert (array[0] == something); // array bounds error? wtf?
}

So that's not a good idea.



More information about the Digitalmars-d mailing list