test: add support for pytest 5.2.0

pytest 5.2.0 added support for callable scopes. In order to distinguish
those (`@pytest.fixture(scope=fn)`) from decorators (`@pytest.fixture`),
it added extra arguments which was not expected by our wrapper. See
https://github.com/pytest-dev/pytest/pull/5776 for the change.

Fixes the following error:

    ImportError while loading conftest 'test/conftest.py'.
    test/conftest.py:42: in <module>
        from fixtures_ws import *
    test/fixtures_ws.py:198: in <module>
        @fixtures.fixture
    test/fixtures.py:36: in fixture
        return pytest.fixture(scope, params, autouse, ids, name)
    E   TypeError: 'bool' object is not iterable

We do not use non-keyword arguments, so it is safe to use `*` instead of
`*args` in the prototype.

Change-Id: I96220e0e85249ad58880e5de75f8987a0fdc16ef
Reviewed-on: https://code.wireshark.org/review/34672
Petri-Dish: Peter Wu <peter@lekensteyn.nl>
Tested-by: Petri Dish Buildbot
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
This commit is contained in:
Peter Wu 2019-10-02 02:26:00 +01:00
parent e21bb9f7ed
commit ba35c23205
1 changed files with 10 additions and 2 deletions

View File

@ -23,17 +23,25 @@ def enable_pytest():
_use_native_pytest = True
def fixture(scope="function", params=None, autouse=False, ids=None, name=None):
def fixture(callable_or_scope=None, *, scope="function", params=None,
autouse=False, ids=None, name=None):
"""
When running under pytest, this is the same as the pytest.fixture decorator.
See https://docs.pytest.org/en/latest/reference.html#pytest-fixture
"""
assert callable(callable_or_scope) or callable_or_scope is None, \
'scope must be a keyword argument'
if _use_native_pytest:
if callable(callable_or_scope):
return pytest.fixture(callable_or_scope)
# XXX sorting of fixtures based on scope does not work, see
# https://github.com/pytest-dev/pytest/issues/4143#issuecomment-431794076
# When ran under pytest, use native functionality.
return pytest.fixture(scope, params, autouse, ids, name)
return pytest.fixture(scope=scope, params=params, autouse=autouse,
ids=ids, name=name)
init_fallback_fixtures_once()
if callable(callable_or_scope):
scope = callable_or_scope
return _fallback.fixture(scope, params, autouse, ids, name)