Skip to content

ScopeMismatch error when adding fixture as parameter #68

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

Closed
ddsl opened this issue Oct 4, 2017 · 7 comments
Closed

ScopeMismatch error when adding fixture as parameter #68

ddsl opened this issue Oct 4, 2017 · 7 comments

Comments

@ddsl
Copy link

ddsl commented Oct 4, 2017

I got a ScopeMismatch error when tried to add fixture to my test fucntion in a class when I add the scope parameter (scope='class' or scope='module'):
My case:

import asyncio
import pytest
import pytest_asyncio
from .database import DB

@pytest.fixture(scope='class')
async def db_setup(request):
    print("\nconnect to db")
    db = await DB.create()
    async def resource_teardown():
        await db.close()
        print("\ndisconnect")
    request.addfinalizer(resource_teardown)
    return db


class TestDB:
    @pytest.mark.asyncio
    async def test_connection(self, event_loop, db_setup):
        db = await db_setup
        with await db._pool as redis:
            res = await redis.ping()
            print(res)
            assert res, "PONG"

And when i run it i get :

 ScopeMismatch: You tried to access the 'function' scoped fixture 'event_loop' with a 'class' scoped request object, involved factories
../../../../../.virtualenvs/jwt_auth/lib/python3.5/site-packages/pytest_asyncio/plugin.py:110:  def wrapper(*args, **kwargs)


==================================== ERRORS ====================================
___________________ ERROR at setup of TestDB.test_connection ___________________
ScopeMismatch: You tried to access the 'function' scoped fixture 'event_loop' with a 'class' scoped request object, involved factories
../../../../../.virtualenvs/jwt_auth/lib/python3.5/site-packages/pytest_asyncio/plugin.py:110:  def wrapper(*args, **kwargs)
============================== 5 tests deselected ==============================
==================== 5 deselected, 1 error in 0.17 seconds =====================

Process finished with exit code 0

I tried to delete the event_loop param but it gave the same error but when i removed scope parameter from the fixture everithing works

@Tinche
Copy link
Member

Tinche commented Oct 4, 2017

Hello,

unfortunately you always need the event_loop fixture, even if you don't ask for it explicitely (because you need it to run your test and fixtures).

From the docs (a little buried I admit): All scopes are supported, but if you use a non-function scope you will need to redefine the event_loop fixture to have the same or broader scope. Async fixtures need the event loop, and so must have the same or narrower scope than the event_loop fixture.

Try redefining the event_loop fixture like this:

@pytest.yield_fixture(scope='class')
def event_loop(request):
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()

@ddsl
Copy link
Author

ddsl commented Oct 4, 2017

Thanks for answer!
I did as you say and got another:

request = <SubRequest 'db_setup' for <Function 'test_connection'>>

    @pytest.fixture(scope='class')
    async def db_setup(request):
        print("\nconnect to db")
>       db = await DB.create()

test_database.py:16: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../jwt_auth_service/database.py:15: in create
    encoding='utf-8')
/home/pel/.virtualenvs/jwt_auth/lib/python3.5/site-packages/aioredis/pool.py:49: in create_pool
    yield from pool.wait_closed()
/home/pel/.virtualenvs/jwt_auth/lib/python3.5/site-packages/aioredis/pool.py:154: in wait_closed
    yield from asyncio.shield(self._close_waiter, loop=self._loop)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <Future pending>

    def __iter__(self):
        if not self.done():
            self._blocking = True
>           yield self  # This tells Task to wait for completion.
E           RuntimeError: Task <Task pending coro=<pytest_fixture_setup.<locals>.wrapper.<locals>.setup() running at /home/pel/.virtualenvs/jwt_auth/lib/python3.5/site-packages/pytest_asyncio/plugin.py:117> cb=[_run_until_complete_cb() at /usr/lib/python3.5/asyncio/base_events.py:164]> got Future <Future pending> attached to a different loop

/usr/lib/python3.5/asyncio/futures.py:361: RuntimeError

But when i remove all scopes params everything works fine.

@Tinche
Copy link
Member

Tinche commented Oct 4, 2017

Hmm.

Could you put together a small example so I can debug it? Simplest possible where the bug is visible, you can just await asyncio.sleep in the fixture.

@ddsl
Copy link
Author

ddsl commented Oct 4, 2017

@Tinche
Copy link
Member

Tinche commented Oct 5, 2017

Hi,

I installed your example into a clean Python 3.6 virtualenv. Didn't get the ScopeMismatch errors, but I did get some warnings with request.addfinalizer. You don't need to use it though, I fixed it up over here (https://gist.github.com/Tinche/96457dedcb34ba6653373469371a95da) and it seems to work. Is this good for you?

@ddsl
Copy link
Author

ddsl commented Oct 6, 2017

Thank you a lot. It didn't help but i think it is indecent to take so much time from someone for the sake of trivia. most likely the problem is in me rather than your code.

@Tinche
Copy link
Member

Tinche commented Oct 9, 2017

Alright, if you want to continue just reopen :)

@Tinche Tinche closed this as completed Oct 9, 2017
alwx added a commit to RasaHQ/rasa that referenced this issue Jan 16, 2020
alecco pushed a commit to alecco/scylladb that referenced this issue Feb 7, 2022
Change default pytest-asyncio event_loop fixture scope to session to
allow async fixtures with scope larger than function.

See pytest-dev/pytest-asyncio#68

Change temporarily table1 fixture to session scope.

Signed-off-by: Alejo Sanchez <[email protected]>
alecco pushed a commit to alecco/scylladb that referenced this issue Feb 11, 2022
Change default pytest-asyncio event_loop fixture scope to session to
allow async fixtures with scope larger than function.

See pytest-dev/pytest-asyncio#68

Change temporarily table1 fixture to session scope.

Signed-off-by: Alejo Sanchez <[email protected]>
alecco pushed a commit to alecco/scylladb that referenced this issue Feb 25, 2022
Change default pytest-asyncio event_loop fixture scope to session to
allow async fixtures with scope larger than function.

See pytest-dev/pytest-asyncio#68

Change temporarily table1 fixture to session scope.

Signed-off-by: Alejo Sanchez <[email protected]>
alecco pushed a commit to alecco/scylladb that referenced this issue Apr 18, 2022
Change default pytest-asyncio event_loop fixture scope to session to
allow async fixtures with scope larger than function.

See pytest-dev/pytest-asyncio#68

Change temporarily table1 fixture to session scope.

Signed-off-by: Alejo Sanchez <[email protected]>
alecco pushed a commit to alecco/scylladb that referenced this issue May 4, 2022
Change default pytest-asyncio event_loop fixture scope to session to
allow async fixtures with scope larger than function.

See pytest-dev/pytest-asyncio#68

Change temporarily table1 fixture to session scope.

Signed-off-by: Alejo Sanchez <[email protected]>
alecco pushed a commit to alecco/scylladb that referenced this issue May 11, 2022
Change default pytest-asyncio event_loop fixture scope to session to
allow async fixtures with scope larger than function.

See pytest-dev/pytest-asyncio#68

Change temporarily table1 fixture to session scope.

Signed-off-by: Alejo Sanchez <[email protected]>
alecco pushed a commit to alecco/scylladb that referenced this issue May 13, 2022
Change default pytest-asyncio event_loop fixture scope to session to
allow async fixtures with scope larger than function.

See pytest-dev/pytest-asyncio#68

Change temporarily table1 fixture to session scope.

Signed-off-by: Alejo Sanchez <[email protected]>
alecco pushed a commit to alecco/scylladb that referenced this issue Jun 7, 2022
Change default pytest-asyncio event_loop fixture scope to session to
allow async fixtures with scope larger than function.

See pytest-dev/pytest-asyncio#68

Change temporarily table1 fixture to session scope.

Signed-off-by: Alejo Sanchez <[email protected]>
alecco pushed a commit to alecco/scylladb that referenced this issue Jun 7, 2022
Change default pytest-asyncio event_loop fixture scope to session to
allow async fixtures with scope larger than function.

See pytest-dev/pytest-asyncio#68

Change temporarily table1 fixture to session scope.

Signed-off-by: Alejo Sanchez <[email protected]>
alecco pushed a commit to alecco/scylladb that referenced this issue Jun 21, 2022
Run test async using a wrapper for Cassandra python driver's future.

The wrapper was suggested by a user and brought forward by @fruch.
It's based on https://stackoverflow.com/a/49351069 .

Redefine pytest event_loop fixture to avoid issues with fixtures with
scope bigger than function (like keyspace).
See pytest-dev/pytest-asyncio#68

Convert sample test_null to async. More useful test cases will come
afterwards.

Signed-off-by: Alejo Sanchez <[email protected]>
alecco pushed a commit to alecco/scylladb that referenced this issue Jun 23, 2022
Run test async using a wrapper for Cassandra python driver's future.

The wrapper was suggested by a user and brought forward by @fruch.
It's based on https://stackoverflow.com/a/49351069 .

Redefine pytest event_loop fixture to avoid issues with fixtures with
scope bigger than function (like keyspace).
See pytest-dev/pytest-asyncio#68

Convert sample test_null to async. More useful test cases will come
afterwards.

Signed-off-by: Alejo Sanchez <[email protected]>
alecco pushed a commit to alecco/scylladb that referenced this issue Jun 23, 2022
Run test async using a wrapper for Cassandra python driver's future.

The wrapper was suggested by a user and brought forward by @fruch.
It's based on https://stackoverflow.com/a/49351069 .

Redefine pytest event_loop fixture to avoid issues with fixtures with
scope bigger than function (like keyspace).
See pytest-dev/pytest-asyncio#68

Convert sample test_null to async. More useful test cases will come
afterwards.

Signed-off-by: Alejo Sanchez <[email protected]>
alecco pushed a commit to alecco/scylladb that referenced this issue Jun 23, 2022
Run test async using a wrapper for Cassandra python driver's future.

The wrapper was suggested by a user and brought forward by @fruch.
It's based on https://stackoverflow.com/a/49351069 .

Redefine pytest event_loop fixture to avoid issues with fixtures with
scope bigger than function (like keyspace).
See pytest-dev/pytest-asyncio#68

Convert sample test_null to async. More useful test cases will come
afterwards.

Signed-off-by: Alejo Sanchez <[email protected]>
alecco pushed a commit to alecco/scylladb that referenced this issue Jun 23, 2022
Run test async using a wrapper for Cassandra python driver's future.

The wrapper was suggested by a user and brought forward by @fruch.
It's based on https://stackoverflow.com/a/49351069 .

Redefine pytest event_loop fixture to avoid issues with fixtures with
scope bigger than function (like keyspace).
See pytest-dev/pytest-asyncio#68

Convert sample test_null to async. More useful test cases will come
afterwards.

Signed-off-by: Alejo Sanchez <[email protected]>
alecco pushed a commit to alecco/scylladb that referenced this issue Jun 28, 2022
Run test async using a wrapper for Cassandra python driver's future.

The wrapper was suggested by a user and brought forward by @fruch.
It's based on https://stackoverflow.com/a/49351069 .

Redefine pytest event_loop fixture to avoid issues with fixtures with
scope bigger than function (like keyspace).
See pytest-dev/pytest-asyncio#68

Convert sample test_null to async. More useful test cases will come
afterwards.

Signed-off-by: Alejo Sanchez <[email protected]>
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