Skip to content Skip to sidebar Skip to footer
Showing posts with the label Mocking

Override Python Function-local Variable In Unittest

I have a method in python (2.7) that does foo, and gives up after 5 minutes if foo didn't work.… Read more Override Python Function-local Variable In Unittest

Mock A Remote Host In Python

I am writing some functions, using paramiko, to execute commands and create files on a remote host.… Read more Mock A Remote Host In Python

Mock.patch And Multiprocessing

I'm struggling to use mock.patch in a multiprocessing environment while without multiprocessing… Read more Mock.patch And Multiprocessing

Mock A Class With Tedious __init__

I have a class that actually connects to a service and does authentication and stuff, but all of th… Read more Mock A Class With Tedious __init__

Python Propertymock Side Effect With Attributeerror And Valueerror

I am trying to mock a property of a class (@property decorator) and have bumped into this incorrect… Read more Python Propertymock Side Effect With Attributeerror And Valueerror

Patch Not Mocking A Module

I'm trying to mock subprocess.Popen. When I run the following code however, the mock is complet… Read more Patch Not Mocking A Module

How To Permanently Mock Return Value Of A Function In Python Unittest

I have a function # foo.py NUM_SHARDS = 10 def get_shard(shard_key: int) -> int return (shar… Read more How To Permanently Mock Return Value Of A Function In Python Unittest

Mock Builtin 'open" Function When Used In Contextlib

I know this question has been asked before, but I have a particular problem, meaning I want the moc… Read more Mock Builtin 'open" Function When Used In Contextlib

Python Patch Object With A Side_effect

I'm trying to have a Mock object return certain values based on the input given. I looked up a … Read more Python Patch Object With A Side_effect

Mock Exception With Side Effect Raised In Class Method And Caught In Calling Method Gives 'did Not Raise'

Using side_effect, I am trying to raise an exception when a mock is called but I get a DID NOT RAIS… Read more Mock Exception With Side Effect Raised In Class Method And Caught In Calling Method Gives 'did Not Raise'

Mocking Instance Attributes

Please help me understand why the following doesn't work. In particular - instance attributes o… Read more Mocking Instance Attributes

Mock An Entire Module In Python

I have an application that imports a module from PyPI. I want to write unittests for that applicati… Read more Mock An Entire Module In Python

Writing A Contextmanager That Patches An Object

In my Python 3 test code, I have a lot of these statements: from unittest.mock import patch user =… Read more Writing A Contextmanager That Patches An Object

Why Does Mocking 'open' And Returning A Filenotfounderror Raise Attributeerror: __exit__?

Testing by mocking open with a FileNotFoundError raises AttributeError: __exit__. Why is this happe… Read more Why Does Mocking 'open' And Returning A Filenotfounderror Raise Attributeerror: __exit__?

Python 3 - Unittest With Multiple Inputs And Print Statement Using Mocking

I'm studying Python and a few weeks ago I had created a game which the user needs to guess the … Read more Python 3 - Unittest With Multiple Inputs And Print Statement Using Mocking

Using Python's Mock Patch.object To Change The Return Value Of A Method Called Within Another Method

Is it possible to mock a return value of a function called within another function I am trying to t… Read more Using Python's Mock Patch.object To Change The Return Value Of A Method Called Within Another Method

Really Weird... Can't Set Attributes Of Built-in/extension Type 'lxml.etree._element'

I've changed attributes for other classes before without issues. _Element is obviously not a bu… Read more Really Weird... Can't Set Attributes Of Built-in/extension Type 'lxml.etree._element'

How Do We Ensure That The Calls In The Mock.call_args_list Contain Calls With Arguments At The Same State Of When The Mock Object Was Called?

from mock import Mock j = [] u = Mock() u(j) # At this point u.call_args_list == [call([])] print u… Read more How Do We Ensure That The Calls In The Mock.call_args_list Contain Calls With Arguments At The Same State Of When The Mock Object Was Called?

Integrating Mock And Patch In A Python Unit Test

I have a class with a few methods that I am writing unit test cases for. For minimum reproducible e… Read more Integrating Mock And Patch In A Python Unit Test

Mocking Grpc Status Code ('rpcerror' Object Has No Attribute 'code') In Flask App

I have to create a unittest that should mock a specific grpc status code (in my case I need NOT_FOU… Read more Mocking Grpc Status Code ('rpcerror' Object Has No Attribute 'code') In Flask App