Skip to content

Verify scope for async fixtures #18

@touilleMan

Description

@touilleMan

Only async fixture with default function scope can work with trio.
Currently, an async fixture with another scope (i.e. module, class or session) will be considered as a regular function and end up as a coroutine in the test function.

This is most likely an inconsistent behavior. A much better solution would be to check the scope of the async fixture before test and raise an error if it's not a correct one.

Following discussion in #17, test file test_fixture_scope.py has been removed until this feature is added to avoid core dump in Python 3.5

import pytest


@pytest.mark.parametrize('scope', ['class', 'module', 'session'])
def test_not_allowed_scopes(testdir, scope):

    testdir.makepyfile(
        """
        import pytest

        @pytest.fixture(scope=%r)
        async def fix1():
            return 'fix1'

        @pytest.mark.trio
        async def test_base(fix1):
            pass  # Crash should have occures before arriving here
    """ % scope
    )

    result = testdir.runpytest()

    result.assert_outcomes(error=1)


@pytest.mark.parametrize('scope', ['function'])
def test_allowed_scopes(testdir, scope):

    testdir.makepyfile(
        """
        import pytest

        @pytest.fixture(scope=%r)
        async def fix1():
            return 'fix1'

        @pytest.mark.trio
        async def test_base(fix1):
            assert fix1 == 'fix1'
    """ % scope
    )

    result = testdir.runpytest()

    result.assert_outcomes(passed=1)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions