mocking

Monkeypatching/mocking the HTTPX external requests

Monkeypatching/mocking the HTTPX external requests Question: I’m trying to monkeypatch the external request. Here is the code of a web endpoint: import httpx, json … @app.get(‘/test’) async def view_test(request): async with httpx.AsyncClient() as client: # sending external request api_response = await client.get( f’https://jsonplaceholder.typicode.com/todos/1′, timeout=10, ) resp = api_response.json() # modifying the result resp[‘foo’] = 0 …

Total answers: 1

Python Unit test a function imported by the function under test

Python Unit test a function imported by the function under test Question: I’m attempting to implement thorough testing of code that calls to a database at various at points. I’m able to mock the calls that occur in the function under test, but I’m now also needing to stub out functions that are imported by …

Total answers: 1

Mock object with pyTest

Mock object with pyTest Question: I have a function I need to test: def get_user_by_username(db, username: str) -> Waiter: """Get user object based on given username.""" user = db.query(Waiter).filter(Waiter.username == username).first() return user Here I try to mock DB call and return correct Waiter object, so my test is: def test_get_user_by_username(mocker): waiter = Waiter(id=10, username="string", …

Total answers: 1

Patch Django EmailMultiAlternatives send() in a Celery Task so that an exception is raised

Patch Django EmailMultiAlternatives send() in a Celery Task so that an exception is raised Question: I want to test a Celery Task by raising an SMTPException when sending an email. With the following code, located in: my_app.mailer.tasks from django.core.mail import EmailMultiAlternatives @app.task(bind=True ) def send_mail(self): subject, from_email, to = ‘hello’, ‘[email protected]’, ‘[email protected]’ text_content = ‘This …

Total answers: 2

Hard to understand return_value for Python Mock Class in the following example

Hard to understand return_value for Python Mock Class in the following example Question: I had a hard time understanding the choice of using return_value. See the following example # my_module.py def query(connection): cursor = connection.cursor() cursor.execute("SELECT name FROM users WHERE id = 1") return cursor.fetchone()[0] # test_query.py import pytest from unittest.mock import Mock # Import …

Total answers: 1

How to mock a function which gets executed during the import time?

How to mock a function which gets executed during the import time? Question: Here the ABC() and obj.print_1() get called during the import time and it prints "making object" and "printed 1" respectively. How can we mock all the three functions, __init__(), print_1(), and print_2()? xyz.py from abc import ABC obj = ABC() obj.print_1() def …

Total answers: 1

How do I remove a handler from a loguru.logger when using pytest?

How do I remove a handler from a loguru.logger when using pytest? Question: I wrote a Thing class that does logging using loguru. At the bottom of the class file I add a handler to the logger. This is what thing.py looks like. from loguru import logger class Thing: def __init__(self): logger.info("Thing created") def __enter__(self): …

Total answers: 1

How to mock a class with nested properties and autospec?

How to mock a class with nested properties and autospec? Question: I’m wondering if it’s possible to mock a class which contains properties by using patch and autospec? The goal in the example below is to mock (recursively) ClassB. Example: # file: class_c.py class ClassC: def get_default(self) -> list[int]: return [1, 2, 3] def delete(self, …

Total answers: 1