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, ...
0
votes
1
answer
108
views
How to `assert_called_with` an object instance?
I want to do a unittest with pytest for this method:
class UserService:
@classmethod
async def get_user_by_login(cls, session: SessionDep, login: str) -> Optional[User]:
sql = ...
2
votes
2
answers
115
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 ...
0
votes
1
answer
75
views
Difference between mock.patch.dict as function decorator and context manager in Python unittest
I have a configuration module, config with data config.data where I want to add a value for testing.
Ideally, I want to use mock.patch.dict as a context manager because the value is a class attribute ...
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
...
1
vote
1
answer
106
views
Unexpected failed assert when changing order of python mocks when mocking function that imports other mocked function
I am implementing a test for a function func1a() in module1.py using mocks.
The function func1a() calls:
one function from module2.py: func2a()
one function from module1.py: func1b()
It appears that ...
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
1
answer
114
views
How can we mock an Object instantiation in python?
I have a scenario where I create a git.repo.base.Repo object in a "someFile.py":
def someFunction():
//some part of code
repo = Repo(path)
repo.head.reference = repo.commit(...
0
votes
0
answers
30
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 ...
0
votes
1
answer
70
views
Why did my mock test work with just a method instead of a full class?
I always thought that you could mock a method on a class only when the class it is coming from is not instantiated in the code under test. And if it is you have to mock the class (just as you have to ...
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 ...
0
votes
1
answer
33
views
Working with python unit tests, tring to patch a class but is not working
I have code with below functionality
src/client.py
class Client1(object):
def foo(self, t):
return f"foo-{t}"
def get_foo(self, type):
return self.foo(type)
src/...
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(&...
0
votes
1
answer
132
views
How to "inject" a local variable when importing a python module?
Is it possible to declare a local variable before it's imported?
For example to get this code to run as expected:
# a.py
# do magic here to make b.foo = "bar"
import b
b.printlocal()
# b....
-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, ...