Skip to content

Cannot use pytest.mark.usefixtures() in pytest.param #4112

Description

@dtomas

Adding Fixtures to a parametrization of a test function via pytest.mark.usefixtures has no effect. The fixtures are not executed and are missing from request.fixturenames.

Example:

import pytest

@pytest.fixture
def c():
    return []

@pytest.fixture
def a(c):
    c.append('a')

@pytest.fixture
def b(c):
    c.append('b')

@pytest.mark.parametrize('expected', [
    pytest.param(['a'], marks=pytest.mark.usefixtures('a'), id='a'),
    pytest.param(['b'], marks=pytest.mark.usefixtures('b'), id='b'),
])
def test_usefixtures_param(request, c, expected):
    assert c == expected
$ pip list
Package        Version
-------------- -------
atomicwrites   1.2.1  
attrs          18.2.0 
more-itertools 4.3.0  
pip            18.1   
pluggy         0.7.1  
py             1.6.0  
pytest         3.8.2  
setuptools     40.4.3 
six            1.11.0 
wheel          0.32.1

Activity

  1. RonnyPfannschmidt commented on Oct 11, 2018

    @RonnyPfannschmidt
    Member

    linking #349 (comment) as it has some more details on the problem

  2. added
    type: bugproblem that needs to be addressed
    topic: fixturesanything involving fixtures directly or indirectly
    on Oct 19, 2018
  3. voronovpetr commented on Nov 15, 2018

    @voronovpetr

    I think I have same problem. Specified fixture as argument for pytest.mark.usefixture will be ignored if this specified fixture has params.

    import pytest
    
    @pytest.fixture
    def data():
        return dict()
    
    
    @pytest.fixture(params=[1, 2])
    def add_data(request, data):
        data['a'] = request.param
    
    
    @pytest.mark.usefixtures('add_data')
    @pytest.fixture
    def completed_data(data):
        data['b'] = 3
        return data
    
    
    def test_data(completed_data):
        assert 'a' in completed_data

    I expect add_data must be executed before start completing completed_data but it does not.
    Also it work (how am I expect) if specify add_data in define of completed_data:

    @pytest.fixture
    def completed_data(data, add_data):
        data['b'] = 3
        return data

    Please say me if I'm wrong.

  4. RonnyPfannschmidt commented on Nov 15, 2018

    @RonnyPfannschmidt
    Member

    marks on fixtures are NOT supported and won't be

  5. nchammas commented on Jul 8, 2020

    @nchammas
    Contributor

    linking #349 (comment) as it has some more details on the problem

    Just for reference, here is the code @dtomas linked to in their original comment: https://github.com/pytest-dev/pytest/blob/3.9.0/src/_pytest/python.py#L418-L421

    Posting this link here since @dtomas's original comment didn't pin the code link to a specific git version or hash. It now points to some unrelated code.

  6. DenisKuplyakov commented on Mar 28, 2022

    @DenisKuplyakov

    It looks like that there is still no support on adding pytest.mark.usefixtures to marks of pytest.param. Are there any news on that?

  7. The-Compiler commented on Mar 28, 2022

    @The-Compiler
    Member

    @DenisKuplyakov I'm not aware of anyone working on this. If there were any news, they would likely be in this issue.

  8. vyahello commented on May 1, 2022

    @vyahello

    Hey guys, do we have any updates on it?

  9. The-Compiler commented on May 1, 2022

    @The-Compiler
    Member

    @vyahello see the comment right above yours. I'm not sure what you're trying to achieve by asking the same thing again a month later...

  10. Zac-HD commented on Jul 22, 2026

    @Zac-HD
    Member

    Closing as resolved: this now produces an explicit ValueError ("pytest.param cannot add pytest.mark.usefixtures") at collection time instead of silently doing nothing, per #12828 (see changelog entry for #4112).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytopic: parametrizerelated to @pytest.mark.parametrizetype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions