开发者

Best way to test instance methods without running __init__

开发者 https://www.devze.com 2022-12-31 11:06 出处:网络
I\'ve got a simple class that gets most of its arguments via init, which also runs a variety of private methods that 开发者_如何学Cdo most of the work.Output is available either through access to obje

I've got a simple class that gets most of its arguments via init, which also runs a variety of private methods that 开发者_如何学Cdo most of the work. Output is available either through access to object variables or public methods.

Here's the problem - I'd like my unittest framework to directly call the private methods called by init with different data - without going through init.

What's the best way to do this?

So far, I've been refactoring these classes so that init does less and data is passed in separately. This makes testing easy, but I think the usability of the class suffers a little.

EDIT: Example solution based on Ignacio's answer:

import types

class C(object):

   def __init__(self, number):
       new_number = self._foo(number)
       self._bar(new_number)

   def _foo(self, number):
       return number * 2

   def _bar(self, number):
       print number * 10

#--- normal execution - should print 160: -------
MyC = C(8)

#--- testing execution - should print 80 --------
MyC = object.__new__(C)
MyC._bar(8)


For new-style classes, call object.__new__(), passing the class as a parameter. For old-style classes, call types.InstanceType() passing the class as a parameter.

import types

class C(object):
  def __init__(self):
    print 'init'

class OldC:
  def __init__(self):
    print 'initOld'

c = object.__new__(C)
print c

oc = types.InstanceType(OldC)
print oc


Why does the usability of the class have to suffer? If all the __init__ is doing is precomputing things so you can expose values as simple variables, change those variables into properties and do the computation (potentially cached/memoized) in the getter. That way your __init__ method is back to doing initialization only and testability is improved.

The downside to this approach is that it might be less performant, but probably not to a significant degree.

0

精彩评论

暂无评论...
验证码 换一张
取 消