'How to mock (monkeypatch) a ContextVar in Pytest?
I am currently using asgi_correlation_id python package in my FastApi project.
This package exposes a ContextVar called correlation_id.
The usage is simple:
from asgi_correlation_id.context import correlation_id
id = correlation_id.get()
Now, it is this particular line that I want to monkeypatch for my pytests.
Here is what all I have tried whole day:
#try1
def mock_correlation_id():
return 123456
monkeypatch.setattr(correlation_id, "get", mock_correlation_id)
#try2
def mock_correlation_id():
return None
monkeypatch.setattr("asgi_correlation_id.context.correlation_id.get", mock_correlation_id)
#try3
def mock_correlation_id():
return mock_id.get()
mock_id = ContextVar("mock_id", default=None)
monkeypatch.setattr("asgi_correlation_id.context.correlation_id.get", mock_correlation_id)
#try4
monkeypatch.setattr("asgi_correlation_id.context.correlation_id.get", lambda: None)
#try5
monkeypatch.setattr("asgi_correlation_id.context.correlation_id.get", lambda: 1234567)
But every single time, I am greeted with the same error:
Attribute error: 'ContextVar' object attribute 'get' is read-only.
/pythonpath/_pytest/monkeypatch.py:360: Attribute Error
I am surprised, I couldn't find any implementation of mocking contextvars on the net. Can anyone help me out on this?
Solution 1:[1]
Would using the set() and reset() methods work for you?
from contextvars import ContextVar
c = ContextVar("my_var")
c.set("old_val")
async def test_var():
assert c.get() == "old_val"
token = c.set("new_val")
assert c.get() == "new_val"
c.reset(token)
assert c.get() == "old_val"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
