All Questions
Tagged with python-unittest.mock monkeypatching
8 questions
1
vote
2
answers
614
views
How to Mock a Function Within a FastAPI Route Using Pytest
I'm working on a FastAPI application and am trying to write some tests for one of my routes. My testing environment cannot make calls to external services. My goal is just to test the parsing of the ...
2
votes
2
answers
239
views
Why does unittest's `mock.patch.start` re-run the function in which the patcher is started?
Let's say we have two files:
to_patch.py
from unittest.mock import patch
def patch_a_function():
print("Patching!")
patcher = patch("to_be_patched.function")
patcher....
0
votes
0
answers
466
views
Patched function return value not being used with pytest
I have a test file where I'm mocking a function to return a boolean value. For example, file1.py is in a directory called service.
file1.py
def is_development():
return is_not_other_env() or ...
0
votes
1
answer
1k
views
Get patch'd object with use of unittest.mock's patch decorator and new
I have the below Python==3.8 code, where I:
Use unittest.mock.patch as a decorator
Preform a patch(..., new=...):
from unittest.mock import patch
class Foo:
pass
class Bar:
pass
@patch(...
1
vote
1
answer
1k
views
How to mock a class in one script from another test script running the first
I have a class SQLES in a separate script, sqles.py, which is imported in main.py and comprises methods to read SQL data and set the results in the class instance.
I am now writing a test script, ...
1
vote
0
answers
2k
views
Python how to patch entire object with patch.object
Python's unittest.mock built-in library provides patch.object, which let's you:
patch the named member (attribute) on an object (target) with a mock object.
With regular patch, you can easily ...
0
votes
0
answers
397
views
Patch global variable once in several modules
Imagine that we have a module one.py with some global variable glob_var=object().
Also I have some other modules on different levels that import glob_var using relative import, such as:
from .one.py ...
1
vote
1
answer
556
views
How to patch method io.RawIOBase.read with unittest?
I've recently learned about unittest.monkey.patch and its variants, and I'd like to use it to unit test for atomicity of a file read function. However, the patch doesn't seem to have any effect.
Here'...