aioredis.FakeRedis calls not being registered by pytest-cov
silviumarcu opened this issue · comments
Describe the bug
Calls made to async fakeredis client lead to lines, up to the next async method call, not being covered.
main.py
import fakeredis
import fakeredis.aioredis
import asyncio
import pytest
fake_client = fakeredis.aioredis.FakeRedis(server=fakeredis.FakeServer())
async def bar():
await asyncio.sleep(2)
print("bar")
async def foo():
await fake_client.set("test_key", 1)
await bar()
cached_val = await fake_client.get("test_key")
print(cached_val)
print("foo")
print("bar")
print("foo")
@pytest.mark.asyncio
async def test_foo():
await foo()
To Reproduce
- run
python -m pytest --cov="." --cov-fail-under=100 --cov-report=term-missing -s -vv main.py
- notice that line containing
await bar()
and everything aftercached_val = ...
being marked as not covered - the printed output is present in the console
Expected behavior
Full code coverage
Desktop (please complete the following information):
- OS: macOS Big Sur
- python version: 3.11.4
- redis-py version: 4.6.0
- full requirements.txt:
fakeredis
pytest
pytest-asyncio
pytest-cov
pytest-xdist
Adding the test:
async def test_coverage(req_aioredis2: redis.asyncio.Redis):
async def bar():
await asyncio.sleep(2)
print("bar")
async def foo():
await req_aioredis2.set("test_key", 1)
await bar()
cached_val = await req_aioredis2.get("test_key")
print(cached_val)
print("foo")
print("bar")
print("foo")
await foo()
I am getting full coverage, both for actual redis and fakeredis
@silviumarcu have you tried what I commented?
@cunla req_aioredis2
fixture is not available (fixture 'req_aioredis2' not found
). But looking at the source, I believe using something similar to test redis related code might help fix the issue.
Any chance of making the fixture available for external usage (e.g. having it in a conftest.py file)?
you can find it in the code: https://github.com/cunla/fakeredis-py/blob/master/test/test_redis_asyncio.py#L32-L63