pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
12.11k stars 2.68k forks source link

Pytest with filterwarnings hangs on uvloop ResourceWarning #11454

Closed Olegt0rr closed 11 months ago

Olegt0rr commented 1 year ago

How to reproduce

1. Enable filterwarnings mode

[tool.pytest.ini_options]
asyncio_mode = "auto"
filterwarnings = [
    "error",
]

2. Create Redis client fixture

import pytest
from redis.asyncio import Redis

@pytest.fixture(name="redis")
async def redis_fixture():
    redis = Redis.from_url("redis://localhost")
    try:
        yield redis
    finally:
        await redis.close()

3. Create simple test

async def test_foo(redis):
    await redis.ping()

4. Install uvloop

Add this to your conftest.py

import uvloop

uvloop.install()

5. Run tests

I hope you know how to do it ;)

Expected result

Since Redis has unclear behaviour of redis.close() method and it doesn't close connection pool, our tests should be failed with ResourceWarning about unclosed TCPTransport

Actual result

Test passed and hanged with infinity loading:

Screenshot 2023-09-23 at 09 11 20

P.S.

Only terminate + re-run with debug mode helps to understand what's going on:

Screenshot 2023-09-23 at 09 16 20
gbarter commented 1 year ago

It has taken me a while to debug, but I think I may be experiencing the same.

Zac-HD commented 1 year ago

I get a different error:

______________________________________________ test_foo ______________________________________________
Traceback (most recent call last):
  File "repro.py", line 19, in test_foo
    await redis.ping()
AttributeError: 'async_generator' object has no attribute 'ping'

More generally, I'm not sure that this is actually a pytest issue. Can you show a minimal reproducing example, where calling your test function with and without -Werror works without pytest, but fails with pytest?

Olegt0rr commented 12 months ago

I forget to copy asyncio_mode = "auto" to ini_options, it seems in your case async fixture was not detected as a fixture. Try to run with asyncio mode

Okay, I'll make an example

github-actions[bot] commented 11 months ago

This issue is stale because it has been open for 14 days with no activity.

github-actions[bot] commented 11 months ago

This issue was closed because it has been inactive for 7 days since being marked as stale.