mocking

How to mocks logging pytest in a fastapi call

How to mocks logging pytest in a fastapi call Question: i am working in a project with fastAPI As the title says, i have an endpoint which calls a logging event when a HTTPException occurs, and when the request is called and finished Something like this: @router.get( "/chat/{chat_id}/messages/", status_code=status.HTTP_200_OK, ) async def get_messages(chat_message: GetMessageValidator = …

Total answers: 1

Writing unit test for a dependent function

Writing unit test for a dependent function Question: I have this function that asks the user for the gender of the voice and then reads the text in that voice. If the user input is not either male or female it raises a value error. I want to write a unit test for this function, …

Total answers: 1

Mock.patch returning MagicMock object causing AssertionError?

Mock.patch returning MagicMock object causing AssertionError? Question: I have a function that I am trying to test in querySomething.py: class QuerySomething: def retrieveIssues(self,token): responses = [] if "customFields" in self._event: if not self.custom_fields: fields = [] else: fields = self.custom_fields else: fields = [] for issueTypeKey, issueTypeValue in self.issueTypes.items(): print(issueTypeKey, ":", issueTypeValue) query = self.getQuery(issueTypeValue, …

Total answers: 1

pytest – how to mock return a value from two different cursor calls in a method

pytest – how to mock return a value from two different cursor calls in a method Question: I have to write unit test in python. I need to mock two different cursor calls in a single method. sql.py file def call_sql(conn, b): query1 = q1 query2 = q2 cur = conn.cursor() run1 = cur.execute(query1).fetchone() run2 …

Total answers: 3

Python unittest to create a mock .json file

Python unittest to create a mock .json file Question: I have function that looks like this: def file1_exists(directory): file1_path = os.path.join(directory, ‘file1.json’) return os.path.exists(file1_path) def file2_exists(directory): log_path = os.path.join(directory, ‘file2.log’) return os.path.exists(file2_path) def create_file1(directory): if file1_exists(directory): return if not file2_exists(directory): return mod_time = os.stat(os.path.join(directory, ‘file2.log’)).st_mtime timestamp = { "creation_timestamp": datetime.datetime.fromtimestamp(mod_time).isoformat() } with open(os.path.join(directory, "file1.json"), "w") …

Total answers: 2

Pytest mock.patch requests AttributeError: does not have the attribute 'json'

Pytest mock.patch requests AttributeError: does not have the attribute 'json' Question: I’m trying to test an api mock call using from unittest.mock import patch. I keep getting an AttributError when I include .json() in my function’s response return (i.e. return response.json()). When I exclude the .json() in my return statement, the test passes. I’m pretty …

Total answers: 2

Python: Getting mocked function value returned, not MagicMock, without adding returnvalue

Python: Getting mocked function value returned, not MagicMock, without adding returnvalue Question: So, basically I want to mock a function imported in another class, and for some reason I can’t retrieve the mocked result without calling returnvalue of the mock. This is the setup: one file, one module, one test class. I want to mock …

Total answers: 1

Changing monkeypatch setattr multiple times

Changing monkeypatch setattr multiple times Question: I am trying to test code that depends on a third party and would like to use monkeypatch to replicate what I expect a request will return. Here is a minimal example of the code that I have. import requests def get_urls(*urls): results = [] for url in urls: …

Total answers: 2

How to retrieve all the content of calls made to a mock?

How to retrieve all the content of calls made to a mock? Question: I’m writing a unit test for a function that takes an array of dictionaries and ends up saving it in a CSV. I’m trying to mock it with pytest as usual: csv_output = ( “NametSurnamern” “EvetFirstrn” ) with patch(“builtins.open”, mock_open()) as m: …

Total answers: 3