[Python-Dev] PEP: Frequently-requested additional features for the `unittest` module

Scott David Daniels Scott.Daniels at Acm.Org
Thu Aug 14 06:18:38 CEST 2008


Ben Finney wrote:
> Michael Foord <fuzzyman at voidspace.org.uk> writes:
> 
>> The full list of changes proposed […] and not shot down was
>> something like:
> […]
> 
>>    assertLessThan
>>    assertGreaterThan
>>    assertLessThanOrEquals
>>    assertGreaterThanOrEquals
> […]
> 
> "Brett Cannon" <brett at python.org> writes:
> 
>> Is any of this really necessary? Isn't this the equivalent of
>> ``assert_(a < b)``? It seems like the only thing you get out of this
>> is a nicer error message, but ``assert_(a < b, 'not %r <= %s' % (a,
>> b))`` is not that complex. And do these cases really come up that
>> often? I would want to see some numbers showing that these are
>> really necessary (in both usage and people even specifying an error
>> message in the first place).
> 
> Though I'm the champion of this PEP, I'll have to refer to Michael
> Foord for his reasoning (or reference to others' reasoning) on this.
> 
My reasoning goes something like this:

     self.assertLessThan(abs(self.base.method(parm1, parm2) -
                             self.base.nominal), 2.54, 'Within an inch')
vs.
     distance = self.base.method(parm1, parm2)
     deviation = self.base.method(parm1, parm2) - self.base.nominal
     self.assert_(abs(deviation) < 2.54, '%s is %s out of spec (more '
                                 'than an inch)' % (distance, deviation)

It is not so much the assertion on values in variables, as it is
assertions on results of calculations.  Unfortunately, my tendency
would be to forgo the "within an inch" over extracting the values into
locals for the purpose of the test; the tests I'm writing currently
focus on the manipulation of hardware for the reader, not on the
extraction of data for the purpose of testing.  Some of the longer
sections are already nearly a couple of pages long; that's how much
code it takes to do a coherent operation on this particular hardware.
I hate that I am using 2 pages now (half a page per is what I'd prefer),
and I'm not willing to bloat the operation with more code.  As a result,
I implemented my own versions of these asserts (Le, Lt, ...) a year or
so ago, and still find them so useful that I'll re-implement them where-
ever I am working without similar tests available.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-Dev mailing list