From 5507752c530033865986880c93154465aac53e92 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 1 May 2020 14:40:15 +0300 Subject: [PATCH] fixtures: remove special cases when deciding when pytest.fixture() is a direct decoration pytest.fixture() can be used either as @pytest.fixture def func(): ... or as @pytest.fixture() def func(): ... or (while maybe not intended) func = pytest.fixture(func) so it needs to inspect internally whether it got a function in the first positional argument or not. Previously, there were was oddity. In the following, func = pytest.fixture(func, autouse=True) # OR func = pytest.fixture(func, parms=['a', 'b']) The result is as if `func` wasn't passed. There isn't any reason for this special that I can understand, so remove it. --- changelog/7253.bugfix.rst | 3 +++ src/_pytest/fixtures.py | 14 ++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 changelog/7253.bugfix.rst diff --git a/changelog/7253.bugfix.rst b/changelog/7253.bugfix.rst new file mode 100644 index 000000000..e73ef663f --- /dev/null +++ b/changelog/7253.bugfix.rst @@ -0,0 +1,3 @@ +When using ``pytest.fixture`` on a function directly, as in ``pytest.fixture(func)``, +if the ``autouse`` or ``params`` arguments are also passed, the function is no longer +ignored, but is marked as a fixture. diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 82a148127..a1574634a 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1152,13 +1152,15 @@ def fixture( if params is not None: params = list(params) - if fixture_function and params is None and autouse is False: - # direct decoration - return FixtureFunctionMarker(scope, params, autouse, name=name)( - fixture_function - ) + fixture_marker = FixtureFunctionMarker( + scope=scope, params=params, autouse=autouse, ids=ids, name=name, + ) - return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name) + # Direct decoration. + if fixture_function: + return fixture_marker(fixture_function) + + return fixture_marker def yield_fixture(