-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
0.19.0: Fixture misses attribute #390
Comments
Env: |
@viralmutant please show the output of the code when you run on python3.10 and pytest-asyncio==0.18.3 |
Here is the output with plugins: forked-1.4.0, cov-3.0.0, asyncio-0.18.3, env-0.6.2, sugar-0.9.5, requests-mock-1.9.3, pyfakefs-4.5.0, xdist-2.5.0
asyncio: mode=legacy
collecting ...
test_bug.py ✓
/CodeBox/turbo-test/lib/python3.10/site-packages/pytest_asyncio/plugin.py:191: DeprecationWarning: The 'asyncio_mode' default value will change to 'strict' in future, please explicitly use 'asyncio_mode=strict' or 'asyncio_mode=auto' in pytest configuration file.
config.issue_config_time_warning(LEGACY_MODE, stacklevel=2)
test_bug.py:12
/CodeBox/turbo/test_bug.py:12: DeprecationWarning: "@coroutine" decorator is deprecated since Python 3.8, use "async def" instead
def release(self):
../turbo-test/lib/python3.10/site-packages/pytest_asyncio/plugin.py:230
.../CodeBox/turbo-test/lib/python3.10/site-packages/pytest_asyncio/plugin.py:230: DeprecationWarning: '@pytest.fixture' is applied to <fixture fake_session, file=/CodeBox/turbo/test_bug.py, line=16> in 'legacy' mode, please replace it with '@pytest_asyncio.fixture' as a preparation for switching to 'strict' mode (or use 'auto' mode to seamlessly handle all these fixtures as asyncio-driven).
warnings.warn(
test_bug.py::test_bug
/CodeBox/turbo/test_bug.py:20: DeprecationWarning: "@coroutine" decorator is deprecated since Python 3.8, use "async def" instead
def _fake_request(method, url, *args, **kwargs):
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
Results (0.84s):
1 passed |
Your test output shows a deprecation warning about asyncio-mode. Starting with pytest-asyncio v0.19 asyncio mode defaults to strict. The error you're seeing is likely caused by wrong fixture decorators. The reproducible example you linked suffers from that exact problem. As @davidandreoletti already mentioned you need to update your fixture definitions if you intend to use strict mode. Strict mode will no longer evaluate async fixtures decorated witih @viralmutant Please check your fixture definitions and let us know if this fixes your issue. |
Yes, after replacing the fixture as The confusing part is that once the version has been upgraded, it just fails and won't tell why. And while being at the earlier version, when a couple of thousand tests are running in CI pipeline, the warnings are ignored |
Hi @seifertm I am trying to use async_generator with fixture in factory mode: @pytest_asyncio.fixture
async def async_client_factory():
async def _factory(x):
print(x) # just to simplify case, we need x as parameter for our factory
async with AsyncClient() as ac:
yield ac
return _factory
@pytest.mark.asyncio
async def test_async_generator_factory(async_client_factory):
async_client = async_client_factory(1)
response = await async_client.get("/test")
assert response.status_code == status.HTTP_200_OK But I get same error:
package versions:
|
just a fixture w/o factory works well @pytest_asyncio.fixture
async def async_client():
async with AsyncClient() as ac:
yield ac
@pytest.mark.asyncio
async def test_async_generator(async_client):
response = await async_client.get("/test")
assert response.status_code == status.HTTP_200_OK |
Interesting, thanks for the report. I'll look into it tomorrow. |
You can't use a yield inside an httpx.AsyncClient like that, you need the See https://discuss.python.org/t/preventing-yield-inside-certain-context-managers/1091 |
@graingert it does not help in case with factory @pytest_asyncio.fixture
async def async_client_factory():
@asynccontextmanager
async def _factory(x):
print(x) # just to simplify case, we need x as parameter for our factory
async with AsyncClient() as ac:
yield ac
return _factory
@pytest.mark.asyncio
async def test_async_generator_factory(async_client_factory):
async_client = async_client_factory(1)
response = await async_client.get("/test")
assert response.status_code == status.HTTP_200_OK similar error
although it works with fixture w/o factory @asynccontextmanager
@pytest_asyncio.fixture
async def async_client():
async with AsyncClient() as ac:
yield ac
@pytest.mark.asyncio
async def test_async_generator(async_client):
response = await async_client.get("/test")
assert response.status_code == status.HTTP_200_OK seems something related to pytest internals |
You need to use async with async_client_factory(1) as async_client:
... |
@kiddick I cannot see anything wrong with the behaviour of pytest-asyncio in the example you provided here. Your fixture returns the From your reaction on the previous comment I assume the issue is resolved for you. I initially left this issue open, because I intended to add a warning when people forgot to update their fixtures. I don't think there's a reliable way to do it, though, so I'll close this issue. |
I am working with
python3.10
andpytest=7.1.2
versionIt seems with the latest version of
pytest-asyncio=0.19.0
a regression has been introduced and this bug is back.Using the same example testcase to reproduce the issue, I observe the same failure
Downgrading pytest-asyncio to version
0.18.3
and the error is gone.The text was updated successfully, but these errors were encountered: