476 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
1
vote
1
answer
124
views
How to test the calling sequence of methods of a class?
Sometimes when I develop by TDD (Test Driven Development) I need to test the calling order of some class methods. In general I write Python code so I'll show the last test case that I have just ...
0
votes
2
answers
585
views
Why is python mock assert_any_call not matching
Given the doc for assert_any_call
I have a log statement I want to assert against
...
logger.warning('Backup quantity is 0, supplied uuids %s, matched machines: %s', uuids, machines)
...
see the ...
0
votes
1
answer
70
views
Python mock not asserting call when target method is called inside another method
I do not manage to do some basic assert_called() in a class where some methods are internally calling other methods.
Example code:
from unittest.mock import Mock
class Foo:
def print1(self) -> ...
0
votes
2
answers
307
views
How to test calls of a mocked dataclass in Python?
I want to write a unit test to a function which instantiates a dataclass twice followed by calling its method. However, the actual calls of the instantiation includes the calling of the method. This ...
0
votes
1
answer
189
views
Need to test an async function that contains multiple async calls. How to mock these calls?
I have a function I need to test
example:
async def post(self):
#bunch of calls
try:
var = await another_func(x, y) #returns int value
if var != 1:
raise exception
...
1
vote
1
answer
191
views
Work out the correct path for Python mock.patch
I am aware the basics of the path for mock.path, but it's increasingly difficult to work out the correct path when the object is encapsulated by layers of dynamic construction, eg. django viewflow how ...
2
votes
0
answers
92
views
Mock a property to return another attribute
With this class:
class Something():
def __init__(self, name) -> None:
self.name=name
@property
def value(self):
# In real life, something long and complicated
...
0
votes
1
answer
35
views
Why mock.patch works on attribute of object that is already imported
I am aware of the importance of path to mock as illustrated here, but consider this Django scenario
models.py
class Proxmox(Model):
@property
def api(self, ...):
....
tasks.py
def ...
1
vote
1
answer
135
views
Mock a method of a patched class not working
I need to test a function that uses a lot of legacy code that I can't touch now. I patched the legacy class (LegacyClass) with the @patch decorator on my test class. The legacy class has a method ...
2
votes
2
answers
883
views
How to mock a constant value
I have a structure like so:
mod1
├── mod2
│ ├── __init__.py
│ └── utils.py
└── tests
└── test_utils.py
where
__init__.py:
CONST = -1
utils.py:
from mod1.mod2 import CONST
def mod_function(...
0
votes
1
answer
748
views
mock patch for the entire test session
tl;dr how to have a mock.patch for a @classmethod last an entire test session instead of only within with scope or function scope?
I want to mock patch a class method. However, I would like to run ...
0
votes
1
answer
32
views
How do I set up and verify Python mock for chained calls?
Say I want to test my calls Greeter which depends a 3rd party class Foo which in turn depends on another class Bar. I need to mock Foo, but how do I set up and verify the chained call self.foo.get_bar(...
-2
votes
1
answer
99
views
How to mock a method of a class instance?
I have code like this:
a.py
from app.cache import Cache
my_cache = Cache(cache_prefix='xxxxx')
b.py
from a import my_cache
class MyApp:
def run_app():
my_cache.get(1,2)
test_b.py
...
0
votes
1
answer
102
views
Can one access the `self` object while mocking a property with PropertyMock using a side_effect function?
I am trying to mock a property and would like to control the return value of the property according to other state in the object.
A small representative example looks like this
import datetime
from ...
1
vote
1
answer
183
views
Django Mock isn't working as expected in Unit test
I'm having a view in which there is a method called can_upload_file that makes external calls. This method is called from view's post (create) method. So, in my unit test I'm trying to test the post ...