[TIP] Test discovery for unittest

Jesse Noller jnoller at gmail.com
Fri Apr 10 11:39:14 PDT 2009


On Fri, Apr 10, 2009 at 2:35 PM, Scott David Daniels
<Scott.Daniels at acm.org> wrote:
> 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

Correction: "Because default mutable args can be a disaster" - should
read "Because default mutable args can are a disaster, and may cause
baldness, liver damage and stroke"

jesse



More information about the testing-in-python mailing list