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

  • SEARCH
  • Home
  • 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 8016937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:41:14+00:00 2026-06-04T20:41:14+00:00

OK, I know this is mentioned in the manual , and probably has to

  • 0

OK,
I know this is mentioned in the manual, and probably has to do with side_effect and/or return_value, but a simple, direct example will help me immensely.

I have:

class ClassToPatch():
   def __init__(self, *args):
       _do_some_init_stuff()

   def some_func():
       _do_stuff()


class UUT():
    def __init__(self, *args)
       resource_1 = ClassToPatch()
       resource_2 = ClassToPatch()

Now, I want to unit test the UUT class, and mock the ClassToPatch. Knowing the UUT class will instantiate exactly two ClassToPatch objects, I want the Mock framework to return a new Mock object for each instantiation, so I can later assert calls on each separately.

How do I achieve this using the @patch decorator in a test case? Namely, how to fix the following code sample?

class TestCase1(unittest.TestCase):

    @patch('classToPatch.ClassToPatch',autospec=True)
    def test_1(self,mock1,mock2):
        _assert_stuff()
  • 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-04T20:41:15+00:00Added an answer on June 4, 2026 at 8:41 pm

    Here’s a quick’n’dirty example to get you going:

    import mock
    import unittest
    
    class ClassToPatch():
       def __init__(self, *args):
           pass
    
       def some_func(self):
           return id(self)
    
    class UUT():
        def __init__(self, *args):
            resource_1 = ClassToPatch()
            resource_2 = ClassToPatch()
            self.test_property = (resource_1.some_func(), resource_2.some_func())
    
    class TestCase1(unittest.TestCase):
        @mock.patch('__main__.ClassToPatch', autospec = True)
        def test_1(self, mock1):
            ctpMocks = [mock.Mock(), mock.Mock()]
            ctpMocks[0].some_func.return_value = "funky"
            ctpMocks[1].some_func.return_value = "monkey"
            mock1.side_effect = ctpMocks
    
            u = UUT()
            self.assertEqual(u.test_property, ("funky", "monkey"))
    
    if __name__ == '__main__':
        unittest.main()
    

    I’ve added test_property to UUT so that the unit test does something useful. Now, without the mock test_property should be a tuple containing the ids of the two ClassToPatch instances. But with the mock it should be the tuple: ("funky", "monkey").

    I’ve used the side_effect property of the mock object so that a different instance of ClassToPatch is returned on each call in the UUT initialiser.

    Hope this helps.

    Edit: Oh, by the way, when I run the unit test I get:

    .
    ----------------------------------------------------------------------
    Ran 1 test in 0.004s
    
    OK
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this issue being mentioned before, but resolutions there didn't apply. I'm having
i know this has been asked but none of the threads had a solution
I know this question has been asked many times. But from what I read,
I know this sounds like a subjective answer, but I will try to make
I know this kind of question has been asked before, for example here: Android:
HI May i know how to make the scroll view as mentioned in this
Know this might be rather basic, but I been trying to figure out how
I know this is kind of easy question but i cant seem to find
I know this must be a rookie question, but how do i accomplish that?
I know this question was being asked many times but I want to be

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.