Questions tagged [python-unittest.mock]
mock object library (part of the Python Standard Library)
python-unittest.mock
369
questions
3
votes
1
answer
44
views
How to mock imported function?
I've tried multiple ways and just can't get the result I'm looking for. It seems like a lot of questions use a different type of mocking with the whole @patch decorators but I've been using this more ...
0
votes
2
answers
47
views
How to test a Pydantic BaseModel with MagicMock spec and wraps
Given this example in Python 3.8, using Pydantic v2:
from pydantic import BaseModel
import pytest
from unittest.mock import MagicMock
class MyClass(BaseModel):
a: str = '123'
# the actual ...
2
votes
1
answer
72
views
Is there a way to mock .strip() for unit testing in Python 2.7's unittest module?
I am using Python 2.7 for a coding project. I'd love to switch to Python 3, but unfortunately I'm writing scripts for a program that only has a python package in 2.7 and even if it had one in 3 our ...
0
votes
1
answer
44
views
How to import and test the target function only without touching other codes in the file?
I'm trying to perform a unit test on each function of a Python file using unittest.
Assuming the file I want to test is called X.py and the file containing the testing code is Y.py.
In file X, there ...
1
vote
1
answer
23
views
How to use unit test's @patch to mock self.attribute.savefig for matplotlib?
I'm trying to figure out how to mock matplotlib's plt.savefig inside a class to test if my GUI's button is actually saving a figure. For simplicity's sake, I will not include the GUI, but only the ...
-1
votes
1
answer
34
views
Function not being mocked unless full path is called
main.py
from path.to.mod import run
def foo(args: str):
run(args)
test.py
@patch("path.to.mod.run")
def verify_args(mock):
foo("bar")
mock.assert_called_once_with(&...
-1
votes
2
answers
70
views
How to run a function multiple times with different return values?
I have a test that intermittently fails during automated builds so I want to add some retries to my tool. However, I'm not sure how to force the tool to fail and verify that it has retried before I ...
0
votes
1
answer
34
views
Python unit testing: function patch does not get applied to consumer that runs in a separate thread
I am trying to test a consumer that consumes messages of a queue and creates the corresponding object in salesforce.
To test the consumer I have to start it in a new thread because, it is an infinite ...
0
votes
1
answer
45
views
side_effect not iterating on Mock in Pytho
I'm trying to obtain different responses by each call on a Mock by using the side_effect attribute. However, I'm obtaining the first one each time. I would like to ask if I can get any help on it. ...
2
votes
1
answer
41
views
Verifying constructor calling another constructor
I want to verify Foo() calls Bar() without actually calling Bar(). And then I want to verify that obj is assigned with whatever Bar() returns.
I tried the following:
class Bar:
def __init__(self, ...
0
votes
1
answer
21
views
Patch call from another class
I have Processor class:
class Processor:
def __init__(self, session_provider):
self.session_provider = session_provider
def run(self):
...
self.session_provider....
0
votes
0
answers
23
views
Mock testing in Python shows an error "no module found named path.csv"
class TestFilterDF(unittest.TestCase):
@patch('plugins.qa_plugins.preprocessing.read_df')
def test_filter_df(self, read_df_mock):
# Mocking read_df function to return a DataFrame ...
0
votes
0
answers
27
views
python unittest mocking a custom exception and triggering it
I can't get my custom mock exception to trigger
def exist_on_p4(path, p4):
""" Checks to see if file exists on p4 """
try:
p4.run_files('-e', path)
...
0
votes
3
answers
38
views
Python unittest how to mock a passed in class
Trying to unittest a method that receives a class as an argument but unsure how to mock it right
I am trying to unittest this method `
def get_local_path(p4_path, p4):
if p4_path.endswith('...'):
...
0
votes
0
answers
23
views
Python unittest.mock.patch: usage of new and side_effect
I am a little confused about the usage of patch and of its arguments: new and side_effect (although this is not a proper argument of patch but is passed to the created MagicMock).
When should I use or ...