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

pytest_cases.parametrize() makes pytest.mark.anyio not working #286

Closed
dybi opened this issue Nov 17, 2022 · 2 comments · Fixed by #301
Closed

pytest_cases.parametrize() makes pytest.mark.anyio not working #286

dybi opened this issue Nov 17, 2022 · 2 comments · Fixed by #301

Comments

@dybi
Copy link

dybi commented Nov 17, 2022

import asyncio

import pytest
import pytest_cases


@pytest.fixture
async def bar_1():
    await asyncio.sleep(0.1)
    return 1


@pytest.fixture
async def bar_2():
    await asyncio.sleep(0.1)
    return 2


@pytest.mark.anyio
@pytest_cases.parametrize("bar", [bar_1, bar_2])
async def test_foo(bar):
    result = await bar()
    assert result > 0

the above code produces the following error:

    def async_warn_and_skip(nodeid: str) -> None:
        msg = "async def functions are not natively supported and have been skipped.\n"
        msg += (
            "You need to install a suitable plugin for your async framework, for example:\n"
        )
        msg += "  - anyio\n"
        msg += "  - pytest-asyncio\n"
        msg += "  - pytest-tornasync\n"
        msg += "  - pytest-trio\n"
        msg += "  - pytest-twisted"
>       warnings.warn(PytestUnhandledCoroutineWarning(msg.format(nodeid)))
E       pytest.PytestUnhandledCoroutineWarning: async def functions are not natively supported and have been skipped.
E       You need to install a suitable plugin for your async framework, for example:
E         - anyio
E         - pytest-asyncio
E         - pytest-tornasync
E         - pytest-trio
E         - pytest-twisted

../../.virtualenvs/sf/lib/python3.10/site-packages/_pytest/python.py:172: PytestUnhandledCoroutineWarning

It looks like using pytest_cases.parametrize() would "cancel" pytest.mark.anyio. The order of the decorators doesn't matter - the same error is produced.

Versions:

anyio                 3.6.1
pytest-cases      3.6.13

@dybi dybi changed the title pytest_cases.parametrize() makes pytest.mary.anyio not working pytest_cases.parametrize() makes pytest.mark.anyio not working Nov 17, 2022
@smarie
Copy link
Owner

smarie commented Feb 23, 2023

Thanks @dybi for this issue !
Indeed, the two plugins do not seem compatible. This is probably due to the fact that pytest_cases.parametrize generates a wrapper function around your test function :

if not isgeneratorfunction(test_func):
# normal test or fixture function with return statement
@wraps(test_func, new_sig=new_sig)
def wrapped_test_func(*args, **kwargs): # noqa
if kwargs.get(fixture_union_name, None) is NOT_USED:
# TODO why this ? it is probably useless: this fixture
# is private and will never end up in another union
return NOT_USED
else:
replace_paramfixture_with_values(kwargs)
return test_func(*args, **kwargs)
else:
# generator test or fixture function (with one or several yield statements)
@wraps(test_func, new_sig=new_sig)
def wrapped_test_func(*args, **kwargs): # noqa
if kwargs.get(fixture_union_name, None) is NOT_USED:
# TODO why this ? it is probably useless: this fixture
# is private and will never end up in another union
yield NOT_USED
else:
replace_paramfixture_with_values(kwargs)
for res in test_func(*args, **kwargs):
yield res

Today, this wrapper does not intelligently turn into an async wrapper when the wrapped function is an async. It therefore would need to be improved. By the way not sure that makefun supports this in wraps so maybe we would need to upgrade it too first.
If you wish to propose PRs do not hesitate !

@smarie
Copy link
Owner

smarie commented Feb 24, 2023

Apparently I already implemented support for coroutines in makefun :)
so this should (maybe) be easier after all

https://smarie.github.io/python-makefun/#generators-and-coroutines

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

Successfully merging a pull request may close this issue.

2 participants