Skip to content

Cannot request parameterized fixture via autouse for non-unittest class #5918

Closed
@blueyed

Description

@blueyed

I've tried this:

import pytest


@pytest.fixture(scope="session", params=("param1", "param2"))
def fix():
    pass


class Test:
    @pytest.fixture(autouse=True)
    def setup(self, request):
        request.getfixturevalue("fix")

    def test_one(self):
        pass

    def test_two(self):
        pass


class Test2:
    def test_one(self, fix):
        pass

    def test_two(self, fix):
        pass

Results in:

============== test session starts ===============
platform linux -- Python 3.7.4, pytest-5.1.4.dev39+g6bfd30d16.d20191006, py-1.8.1.dev11+g34f716fe, pluggy-0.12.0 -- …/Vcs/pytest/.venv/bin/python
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('…/Vcs/pytest/.hypothesis/examples')
rootdir: …/Vcs/pytest, inifile: tox.ini
plugins: hypothesis-4.23.8, forked-1.0.2, xdist-1.29.0
collected 6 items

t_fixture_param.py::Test::test_one ERROR   [ 16%]
t_fixture_param.py::Test::test_two ERROR   [ 33%]
t_fixture_param.py::Test2::test_one[param1] PASSED [ 50%]
t_fixture_param.py::Test2::test_two[param1] PASSED [ 66%]
t_fixture_param.py::Test2::test_one[param2] PASSED [ 83%]
t_fixture_param.py::Test2::test_two[param2] PASSED [100%]

===================== ERRORS =====================
________ ERROR at setup of Test.test_one _________
The requested fixture has no parameter defined for test:
    t_fixture_param.py::Test::test_one

Requested fixture 'fix' defined in:
t_fixture_param.py:5

Requested here:
t_fixture_param.py:12
________ ERROR at setup of Test.test_two _________
The requested fixture has no parameter defined for test:
    t_fixture_param.py::Test::test_two

Requested fixture 'fix' defined in:
t_fixture_param.py:5

Requested here:
t_fixture_param.py:12
============ short test summary info =============
ERROR t_fixture_param.py::Test::test_one
ERROR t_fixture_param.py::Test::test_two
=========== 4 passed, 2 error in 0.07s ===========

The error happens in

param = funcitem.callspec.getparam(argname)
:

> (Pdb++) funcitem.callspec.getparam(argname)
*** AttributeError: 'Function' object has no attribute 'callspec'

Not sure if that's correct there, or if the attribute error for funcitem.callspec is hiding something else.

I've seen #3095 - but this is not a unittest class (I've also tried renaming "setup" to make sure it is not triggered through that name).

Apart from that I think the error message makes no sense here, does it?

The requested fixture has no parameter defined for test:

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectly

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions