I have the following structure for the project
myapp
|
-contrib
|
- helpers.py
- views
|
- app_view.py
In helpers.py
I have the following method
def method(**kwargs) -> int:
# Some code here
requests.post(
url=f"{REMOTE_SERVER}/sc",
json=kwargs,
timeout=5
)
In app_view.py
I have the following implementation. I am calling the method inside a thread and I need to mock it using pytest to check whether it's being called once and called with certain number of parameters.
import myapp.contrib.helpers import method
class Score(MethodView):
@prepare_req
def post(self, **kwargs):
"""
Post Score
"""
response_data = dict()
# Some logic here
self.filter_score(kwargs)
return jsonify(response_data)
def filter_score(self, **kwargs)
thread = threading.Thread(target=method, kwargs=event_data)
thread.start()
Using pytest
I am trying to mock the method()
as follows.
def test_score(client, mocker):
url = "sc/1.1/score"
patched_method = mocker.patch(
"myapp.contrib.helpers.method"
)
client.post(url, json=issue_voucher_post_data)
patched_method.assert_called_once() # <- this is getting false and not mocking the method
Tried the below as well but it wont patch properly
patched_method = mocker.patch(
"myapp.views.app_view.method"
)
But it will call the corresponding view
How to mock a function used inside a thread in a view which is available in another module using pytest ?