Best way to test instance methods without running __init__

I have a simple class that gets most of its arguments through init, which also runs many private methods that do most of the work. The output is available either through access to object variables or to public methods.

Here's the problem - I would like my unittest framework to directly invoke private methods called init with different data, without passing init.

What is the best way to do this?

So far, I have refactored these classes so that init does less and the data is transferred separately. This makes testing easier, but I think the usability of the class suffers a bit.

EDIT: An 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)
+3
2

object.__new__(), . types.InstanceType(), .

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
+3

? __init__ , , , ( /memoized ) . , __init__ .

, , , , .

+4

Source: https://habr.com/ru/post/1746032/


All Articles