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

file.py is marked to be run in an event loop with scope class, but is not part of any class. #732

Closed
menkotoglou opened this issue Dec 29, 2023 · 4 comments

Comments

@menkotoglou
Copy link

Hey, these may be related to other issues already opened like #706 or #718. Migrating from 0.21.0 to 0.23.2 (tried also 0.22.0 in between and I don't face any issues with this version).

I have my test_file.py file like this:

import pytest
from fastapi.encoders import jsonable_encoder
from httpx import AsyncClient

from tests.utils.projects import PostProjectPayloadFactory
from tests.utils.users import CURRENT_USER


@pytest.fixture(scope="class")
def test_projects_state(request):
    """Simple fixture to store tests' state in a class."""

    class TestProjectTest(object):
        def __init__(self):
            self.project = None

    # make it available directly in the class
    request.cls.state = TestExampleTest()


@pytest.mark.usefixtures(
    "logged_user",
)
@pytest.mark.random_order(disabled=True)
class TestExamples(object):

    async def test_1_post_project(
        self, some_code: some_type, cli: AsyncClient
    ):
        foo code

And what I get after running pytest is this

ERROR tests/e2e/test_file.py - _pytest.config.exceptions.UsageError: test_file.py is marked to be run in an event loop with scope class, but is not part of any class.

Opened a new issue, because I didn't see these changes before and it's the first time I see this error, although I extensively searched for it.

Thank you in advance!

@seifertm
Copy link
Contributor

seifertm commented Jan 1, 2024

I'm unable to run your specific code example, because it references a bunch of other fixtures and classes I don't know about. However, I can try to explain what triggers the error message.

As of v0.23 pytest-asyncio provides one asyncio event loop for collector or item in your test suite (i.e. session, packages, modules, …). This allows tests to run in loops with different scopes. When a test is marked to run in class scope, but there is not surrounding class, pytest-asyncio will report a usage error. For example, the following code would trigger the error you're seeing:

import asyncio
import pytest

@pytest.mark.asyncio(scope="class")
async def test_has_no_surrounding_class():
    pass

In your code, it seems to be the case that the error message is triggered for an async fixture. Unfortunately, pytest-asyncio currently assumes that the fixture scope is related to the event loop scope and couples both together. So maybe you're trying to execute a fixture with scope "class", but the fixture has no surrounding test class? If this is true, then this is a duplicate of #706.

@menkotoglou
Copy link
Author

Thank you very much for your response @seifertm. The way I understand this, since it's a duplicate of #706, we identify it as a bug, therefore we'll be closing the issue and wait for a future fix on the above?

@seifertm
Copy link
Contributor

seifertm commented Jan 2, 2024

Thanks for the quick response. Yes, let's close this as a duplicate of #706.

@seifertm seifertm closed this as not planned Won't fix, can't repro, duplicate, stale Jan 2, 2024
@menkotoglou
Copy link
Author

Thanks a lot @seifertm. I'll be following #706 for updates on this.

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