[TIP] Test discovery for unittest

Scott David Daniels Scott.Daniels at Acm.Org
Fri Apr 10 11:35:02 PDT 2009


Olemis Lang wrote:
> On Fri, Apr 10, 2009 at 1:18 PM, Scott David Daniels wrote:
>   
>> Olemis Lang wrote:> class PackageTestLoader(unittest.TestLoader):
>>     
>>>    ...
>>>    def __init__(self, pattern=defaultPattern, loader=defaultTestLoader,
>>>                 impall=False, globs={}, ns={},
>>>                 ):
>>>        ...
>>>        self.locals = ns
>>>        self.globs = globs
>>>        ...
>>>       
>> I'd suggest instead:
>>  def __init__(self, pattern=defaultPattern, loader=defaultTestLoader,
>>               impall=False, globs=None, ns=None):
>>      if ns is None:
>>          ns = {}
>>      if globs is None:
>>          globs = {}
>>      ...
>>      self.locals = ns
>>      self.globs = globs
>>      ...
>>     
>
> Please ... why ?
Because default mutable args can be a disaster.  Although it is unlikely
that two instance of PackageTestLoader will be created, if you ever do,
and they are both created without specifying locals and globals, they will
share the same set of globals and locals, and the second one will start with 
a bunch of things loaded that you'll never expect.  This can make tests that 
should fail on NameErrors pass.

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




More information about the testing-in-python mailing list