383 questions
1
vote
0
answers
61
views
unit testing in DRF, Error mock.patch transaction.atomic with unittest.mock.patch
@transaction.atomic
def deposit(user: User, account_number: str, amount: float) -> None:
account = get_object_or_404(
Account.objects.select_for_update(), account_number=account_number, ...
2
votes
2
answers
110
views
How to correctly patch an async function for unit tests in pytest
I am coding a FastAPI app with async functions. My issue is, I can't properly mock async function with the mocker.patch from pytest-mock nor with AsyncMock from unittest.
The issue is, I can't test ...
1
vote
1
answer
64
views
How can I apply side effects to a function of an instance that is created in a function that is tested?
I have a custom class in the file MyClass.py
class MyClass:
def __init__(self):
self.fetched_data = False
def get_data(self):
self.fetched_data = True
...
2
votes
1
answer
44
views
Test that unittest.Mock was called with some specified and some unspecified arguments [duplicate]
We can check if a unittest.mock.Mock has any call with some specified arguments.
I now want to test that some of the arguments are correct, while I do not know about the other ones.
Is there some ...
0
votes
0
answers
23
views
Using unittest.Mock in Odoo 17
In odoo 17 I need to use unitest.mock to mock the return value of a function
The function is called cfdi_cancel.
It is located in mymodulename module, inside that module is the 'models' folder, inside ...
1
vote
1
answer
42
views
Can you use pytest.fixture and unittest.mock decorators together?
I am trying to call four decorators on my unit tests to easily allow code to be refactored: pytest.fixture, unittest.mock.patch, pytest.mark.parameterize, pytest.mark.asyncio.
I currently am calling ...
1
vote
1
answer
106
views
How to patch a 3rd party lib in a unit test with FastAPI client
I have an app/main.py for FastAPI which does:
import qdrant_client as QdrantClient
...
qdrant_client = QdrantClient(url=...)
qdrant_client.create_collection(...)
...
app = FastAPI()
...
@app.get(&...
-1
votes
1
answer
70
views
Python unittest.mock patch fail with F() expressions can only be used to update, not to insert
A minimal working example is available at https://github.com/rgaiacs/django-mwe-magicmock.
When using Django, I use Model.clean() to validate the form submitted by the user. During the validation, ...
1
vote
0
answers
32
views
unittest.mock patch decorator is not called depending on the pytest target
Running
docker compose -f docker-compose.local.yml run --rm django pytest ./project/app/
the following test passes perfectly, meaning patch method is being used
@patch("project.app....
1
vote
1
answer
188
views
How to patch function within pandas DataFrame.apply call
I'm trying to write a test for a class which performs a pandas apply. Here's a simplified version:
import pandas as pd
class Foo:
def bar(self, row):
return "BAR"
def apply(...
-1
votes
2
answers
56
views
How do I test instantiation outside a function with patching in Python
I have a file that looks roughly like this:
bar.py
from foo import foo # function that returns an int
from dog import Dog
dog_foo = 4 * foo()
my_dog = Dog(dog_foo)
...
I want to test that the ...
0
votes
2
answers
75
views
Assert that two files have been written correctly
How would I assert that this function wrote to those files in tests?
def write() -> None:
with open('./foo', 'w') as f:
f.write('fooo')
with open('./bar', 'w') as f:
f.write(...
3
votes
1
answer
56
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 ...
2
votes
1
answer
124
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 ...
1
vote
1
answer
112
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
2
answers
85
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
80
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. ...
0
votes
1
answer
22
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
3
answers
63
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('...'):
...
1
vote
0
answers
128
views
Mocking GraphCypherQAChain in Python unit test cases
chain.py
----------------------------------
from langchain.chains import GraphCypherQAChain
from langchain_openai import ChatOpenAI
from langchain_community.graphs import Neo4jGraph
from langchain....
0
votes
0
answers
40
views
Define return value after chained mocks
I am using unittest.mock to test my Django application.
The set-up is the following:
I want to test a function foo
foo uses a method X which is a constructor from an external package
X is called in ...
1
vote
0
answers
50
views
Why mock is not applied on a method which called within thread after testcase finishes
I have these two classes:
one.py
class One:
def __init__(self) -> None:
pass
def startS(self):
self._doS()
def _doS(self):
print("Inside doS ...
0
votes
1
answer
52
views
How to send extra parameter to unittest's side_effect in Python?
I'm using side_effect for dynamic mocking in unittest.
This is the code.
// main functionn
from api import get_users_from_api
def get_users(user_ids):
for user_id in user_ids:
res = ...
0
votes
1
answer
74
views
Dynamic mocking using patch from unittest in Python
I'm going to mock a Python function in unit tests.
This is the main function.
from api import get_users_from_api
def get_users(user_ids):
for user_id in user_ids:
res = get_users_from_api(...
0
votes
0
answers
62
views
Mock a method called on a mock object (execute method of cursor object generated by a mocked connection)
I want to make a unit test for the following code.
But i can not try to correctly mock the cursor of my mocked connection.
In the unit test, I mock first the connection of my tested code. But the ...