Skip to content
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

pytestmark not being applied for pytest-asyncio #1943

Closed
Tinche opened this issue Sep 17, 2016 · 3 comments
Closed

pytestmark not being applied for pytest-asyncio #1943

Tinche opened this issue Sep 17, 2016 · 3 comments

Comments

@Tinche
Copy link
Member

Tinche commented Sep 17, 2016

Hi,

I'm the maintainer of pytest-asyncio.

Pytest-asyncio uses a marker, @pytest.mark.asyncio, with some optional parameters. To save typing, I was going to suggest my users use something like this:

import asyncio
import pytest

pytestmark = pytest.mark.asyncio(forbid_global_loop=True)

async def test_subprocess(event_loop):
    await asyncio.sleep(0)

instead of:

import asyncio
import pytest

@pytest.mark.asyncio(forbid_global_loop=True)
async def test_subprocess(event_loop):
    await asyncio.sleep(0)

These two should be equivalent, right? But they aren't. pytest-asyncio uses a pytest_pycollect_makeitem hook:

@pytest.mark.tryfirst
def pytest_pycollect_makeitem(collector, name, obj):
    if collector.funcnamefilter(name) and _is_coroutine(obj):
        item = pytest.Function(name, parent=collector)
        if ('asyncio' in item.keywords or
           'asyncio_process_pool' in item.keywords):
            return list(collector._genfunctions(name, obj))

but using the pytestmark approach, the 'asyncio' keyword isn't in item.keywords.

I'm not saying pytest is doing something wrong here, I'm saying help me make this work. :)

@nicoddemus
Copy link
Member

Hi @Tinche,

My guess is that the mark propagation mechanism is not picking up async functions, so it is a bug in pytest. The code in question is in transfer_markers function, I believe.

Sorry for not being able to help out more right now.

@Tinche
Copy link
Member Author

Tinche commented Sep 17, 2016

Haha, s'alright, it's not an urgent thing. Maybe I'll dig around in the pytest source too, try to see what I can find.

@Tinche
Copy link
Member Author

Tinche commented Oct 16, 2016

It turned out pytest applies the module- and class-level markers after collection, so I just made pytest-asyncio do this manually when I need it to happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants