Skip to content

Instantly share code, notes, and snippets.

@santiagobasulto
Created July 5, 2012 23:05
Show Gist options
  • Star 41 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save santiagobasulto/3056999 to your computer and use it in GitHub Desktop.
Save santiagobasulto/3056999 to your computer and use it in GitHub Desktop.
Mocking private methods in python
""" This is a simple gist to show how to mock
private methods. I've got lots of questions
regarding this topic. Most people seems confused.
Hope it helps.
"""
import unittest
import mock
class Car:
def __private(self):
return 1
def no_private(self):
return self.__private()
class CarTest(unittest.TestCase):
def test_exception_raises(self):
c = Car()
with self.assertRaises(AttributeError):
c.__private()
def test_car_works(self):
c = Car()
self.assertEqual(c.no_private(), 1)
def test_mock_private(self):
c = Car()
with mock.patch.object(c, '_Car__private', return_value=3) as method:
c.no_private()
method.assert_called_once_with()
if __name__ == "__main__":
unittest.main()
@rahulrajaram
Copy link

Awesome, thanks.

In particular, your test_mock_private example helped me.

@namaggarwal
Copy link

Thanks. This is what I was looking for :)

@josemarcosrf
Copy link

Thanks, this is perfect!

@BrianBohe
Copy link

Thanks a lot!

@BISASAM
Copy link

BISASAM commented Jun 5, 2018

+1

@MSabherwal
Copy link

ty!

@dnyaneshwarmore
Copy link

dnyaneshwarmore commented Oct 5, 2018

wow very simple and clear

@mellertson
Copy link

Excelsior!!

@BigNerd
Copy link

BigNerd commented Dec 17, 2019

this works for me as well (and is nicer when you have to patch more than one private method):

@mock.patch.object(Car, "_Car__private")
def test_mock_private(self, method):
    c = Car()
    c.no_private()
    method.assert_called_once_with() 

@carantunes
Copy link

this works for me as well (and is nicer when you have to patch more than one private method):

@mock.patch.object(Car, "_Car__private")
def test_mock_private(self, method):
    c = Car()
    c.no_private()
    method.assert_called_once_with() 

🥇

@cuZzior
Copy link

cuZzior commented Feb 21, 2020

great stuff 😄 thank you

@damorosodaragona
Copy link

There is a way to mock private class attribute?
For example in the following code it's possibile patch __private_attribute to set it to 2?

class Car:    
	__init__(self):
		self.__private_attribute = 1

	def use_private_attribute:
		If self.__private_attribute > 2:
			return True
		else:
			return False

	def __private(self):      
		  return 1

   	def no_private(self):       
		return self.__private()

@santiagobasulto
Copy link
Author

@damorosodaragona that's an instance method, and it should work in the same way.

@damorosodaragona
Copy link

@santiagobasulto if i try to patch self.__private_attribute in the same way that you shown i got "no attribute error" from mock

@santiagobasulto
Copy link
Author

@damorosodaragona use this:

from mock import patch, PropertyMock
with mock.patch.object(c, '_Car __private_attribute', new_callable=PropertyMock) as mock:
    mock.return_value = 4. # the value of the attr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment