Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8564661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:14:57+00:00 2026-06-11T17:14:57+00:00

The key to this question is aiding unit-testing. If I have a busy __init__

  • 0

The key to this question is aiding unit-testing. If I have a busy __init__ (i.e. __init__ that does complex initialization), I cannot simply instantiate an object of a class, but I need to mock/stub out all methods invoked on dependencies within the __init__.

To illustrate this problem, here is example:

class SomeClass(object):
    def __init__(self, dep1, dep2, some_string):
        self._dep1 = dep1
        self._dep2 = dep2
        self._some_string = some_string

        # I would need to mock everything here (imagine some even more
        # complicated example)
        for dep2element in self._dep2:
            dep2element.set_dep(dep1)
        self._dep1.set_some_string(some_string)

    def fun1(self):
        ...
    def fun2(self):
        ...
    def fun3(self):
        ...

To test the fun* functions, every test must perform the complex construction.

class TestSomeClass(TestCase):
    def create_SomeClass(self, some_string):
        dep1 = Mock()
        # mock everything required by SomeClass' constructor

        dep2 = Mock()
        # mock everything required by SomeClass' constructor

        return SomeClass(dep1, dep2, some_string)

    def test_fun1(self):
        sc = self.create_SomeClass('some string')
        ...

    def test_fun2(self):
        sc = self.create_SomeClass('some other string')
        ...

    def test_fun3(self):
        sc = self.create_SomeClass('yet another string')
        ...

I find this redundant and would like to know how this problems can be elegantly handled in python, if not by moving the work from the constructor.

SOLUTION:

As @ecatmur suggested, to test some specific function, this code should do the trick:

def test_some_method():
    mobject = Mock(SomeClass)
    SomeClass.fun1(mobject)

With this approach all the methods will be mocked out. If fun1 calls some other method you want executed (e.g. fun2) you can do it like this:

def test_some_method():
    mobject = Mock(SomeClass)
    mobject.fun2 = SomeClass.fun2.__get__(mobject)
    SomeClass.fun1(mobject)

SomeClass.fun2.__get__(mobject) will produce instancemethod which will provide the correct binding.

¡Viva el Python!

ORIGINAL QUESTION:

Original question was centered around moving the work done in __init__ to the separate init method and different problems revolving that approach. My usual approach is to make this

class SomeClass(object):
    def __init__(self, dep1, dep2, some_string)
        self._dep1 = dep1
        self._dep2 = dep2

        # lots of mumbo-jumbo here...

become this

class SomeClass(object):
    def __init__(self, dep1, dep2)
        self._dep1 = dep1
        self._dep2 = dep2

    def initiate(self, some-string)
        # lots of mumto-jumbo here...

General sentiment was that moving work from __init__ is not a common practice and would be meaningless to seasoned python developers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:14:58+00:00Added an answer on June 11, 2026 at 5:14 pm

    If you write initialization functions separate from __init__ then experienced developers will certainly see your code as a kid’s playground.

    If you’re concerned about being able to create objects that look like instances of your class without running the __init__ method, then use Mock:

    def test_some_method():
        mock_object = Mock(MyClass)
        MyClass.some_method(mock_object)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is about the public key that every .NET assembly has AFAIK (looking
This is going to be a very simple question, I have code that looks
This question here: MySQL: INSERT IGNORE or ON DUPLICATE KEY UPDATE with checking multiple
In his answer to this question , BlackBear suggested replacing string y = Session[key]
I have this code: _trackit: function(){ for(var key in this.items.sublinks){ switch(key){ case 'shoes': for(var
this is more a theoretical question i asked myself. I remembered that BinarySearch of
This is sufficiently important to me that I'm opening the question up to a
I have 2 arrays that I want to merge based on a key's value
This is almost exactly the same as this question json get key names as
This question involves Delphi and XE specifically deprecating Suspend and Resume. I have read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.