DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How are you handling the data revolution? We want your take on what's real, what's hype, and what's next in the world of data engineering.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Automating Kubernetes RBAC Sync With LDAP Entitlements Using Python
  • Building AI Agents With Python, LangChain, and GPT APIs
  • Advancing Robot Vision and Control
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques

Trending

  • Zero-Trust AI: Applying Cybersecurity Best Practices to AI Model Development
  • The Missing Layer in AI Pipelines: Why Data Engineers Must Think Like Product Managers
  • Parallel Data Conflict Resolution in Enterprise Workflows: Pessimistic vs. Optimistic Locking at Scale
  • Containerizing AI: Hands-On Guide to Deploying ML Models With Docker and Kubernetes
  1. DZone
  2. Coding
  3. Languages
  4. Another approach to mocking properties in Python

Another approach to mocking properties in Python

By 
Michael Foord user avatar
Michael Foord
·
Nov. 18, 11 · Interview
Likes (0)
Comment
Save
Tweet
Share
28.3K Views

Join the DZone community and get the full member experience.

Join For Free

emoticon:waffle mock is a library for testing in python. it allows you to replace parts of your system under test with mock objects. the main feature of mock is that it's simple to use, but mock also makes possible more complex mocking scenarios.

this is my philosophy of api design as it happens: simple things should be simple but complex things should be possible .

several of these more complex scenarios are shown in the further examples section of the documentation. i've just updated one of these, the example of mocking properties.

properties in python are descriptors . when they are fetched from the class of an object they trigger code that is then executed. the code that is executed is the method that you have wrapped as the property getter.

note that there is a special rule for attribute lookup on certain types of descriptors, which include properties. even if an instance attribute exists, the class attribute will still be used instead. this is an exception to the normal attribute lookup rule that instance attributes are fetched in preference to class attributes. this is important because it means that when you want to mock a property you have to do it on the class and can't simply stick an attribute onto an object.

if you're using mock 0.7, with its support for magic methods , we can patch the property name and add a __get__ method to our mock. the presence of the __get__ method makes it a descriptor, so we can use it as a property:

>>> from mock import mock, patch
>>> class foo(object):
...    @property
...    def fish(self):
...      return 'fish'
...
>>> with patch.object(foo, 'fish') as mock_fish:
...   mock_fish.__get__ = mock(return_value='mocked fish')
...   foo = foo()
...   print foo.fish
...
mocked fish
>>> mock_fish.__get__.assert_called_with(mock_fish, foo, foo)


in this example mock_fish replaces the property and the mock we put in place of __get__ becomes the mocked getter method. as we're patching on the class this affects all instances of foo .

there's no point in using magicmock for this. magicmock normallly makes using the python protocol methods simpler by preconfiguring them. as you can see from the example above, mocking __get__ is supported but it isn't hooked up by default. it wouldn't be helpful if mocking any method on a class replaced it with a mock that acted as a descriptor, so if you want a mock to behave as a descriptor then you have to configure __get__ , __set__ and __delete__ yourself.

here's an alternative approach that works with all recent-ish versions of mock:

>>> from mock import mock, patch
>>> class propertymock(mock):
...   def __get__(self, instance, owner):
...     return self()
...
>>> prop_mock = propertymock()
>>> with patch.object(foo, 'fish', prop_mock):
...   foo = foo()
...   prop_mock.return_value = 'mocked fish'
...   print foo.fish
...
mocked fish
>>> prop_mock.assert_called_with()


as an added bonus, both of these examples work even if the foo instance is created outside of the patch block. so long as the code using the property is executed whilst the patch is in place the attribute lookup will find our mocked version.


source: http://www.voidspace.org.uk/python/weblog/arch_d7_2011_06_04.shtml

Property (programming) Python (language)

Opinions expressed by DZone contributors are their own.

Related

  • Automating Kubernetes RBAC Sync With LDAP Entitlements Using Python
  • Building AI Agents With Python, LangChain, and GPT APIs
  • Advancing Robot Vision and Control
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: