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 6849061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:56:38+00:00 2026-05-27T00:56:38+00:00

I am using mock with Python and was wondering which of those two approaches

  • 0

I am using mock with Python and was wondering which of those two approaches is better (read: more pythonic).

Method one: Just create a mock object and use that. The code looks like:

def test_one (self):
    mock = Mock()
    mock.method.return_value = True 
    
    # This should call mock.method and check the result. 
    self.sut.something(mock) 

    self.assertTrue(mock.method.called)

Method two: Use patch to create a mock. The code looks like:

@patch("MyClass")
def test_two (self, mock):
    instance = mock.return_value
    instance.method.return_value = True
    
    # This should call mock.method and check the result.
    self.sut.something(instance) 

    self.assertTrue(instance.method.called)

Both methods do the same thing. I am unsure of the differences.

Could anyone enlighten me?

  • 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-05-27T00:56:39+00:00Added an answer on May 27, 2026 at 12:56 am

    mock.patch is a very very different critter than mock.Mock. patch replaces the class with a mock object and lets you work with the mock instance. Take a look at this snippet:

    >>> class MyClass(object):
    ...   def __init__(self):
    ...     print 'Created MyClass@{0}'.format(id(self))
    ... 
    >>> def create_instance():
    ...   return MyClass()
    ... 
    >>> x = create_instance()
    Created MyClass@4299548304
    >>> 
    >>> @mock.patch('__main__.MyClass')
    ... def create_instance2(MyClass):
    ...   MyClass.return_value = 'foo'
    ...   return create_instance()
    ... 
    >>> i = create_instance2()
    >>> i
    'foo'
    >>> def create_instance():
    ...   print MyClass
    ...   return MyClass()
    ...
    >>> create_instance2()
    <mock.Mock object at 0x100505d90>
    'foo'
    >>> create_instance()
    <class '__main__.MyClass'>
    Created MyClass@4300234128
    <__main__.MyClass object at 0x100505d90>
    

    patch replaces MyClass in a way that allows you to control the usage of the class in functions that you call. Once you patch a class, references to the class are completely replaced by the mock instance.

    mock.patch is usually used when you are testing something that creates a new instance of a class inside of the test. mock.Mock instances are clearer and are preferred. If your self.sut.something method created an instance of MyClass instead of receiving an instance as a parameter, then mock.patch would be appropriate here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the mock-0.6 library from http://www.voidspace.org.uk/python/mock/mock.html to mock out a framework for testing,
I've been trying to get to mock a method with vararg parameters using Mockito:
I'm using Python 2.6.5 with Mock 0.7.2 and have a unit test where I
I installed Python Mock module using PIP. When I try to import mock running
I just started using mock objects (using Java's mockito) in my tests recently. Needless
I know how to mock methods in Python using flexmock, like flexmock(subprocess).should_receive('call').replace_with(my_func) How does
I'm using python-mock to mock out a file open call. I would like to
I am using a mock object in RhinoMocks to represent a class that makes
I am using Moq to mock my Repository layer so I can unit test.
I want to make a mock installer using NSIS so we can demo what

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.