Skip to content

Parametrized fixture content depends on test function order.  #5693

Closed
@LaurentMarchelli

Description

@LaurentMarchelli

Fixture parametrized at the function definition, parameter scope='class'

When the same fixture (scope='class') is used by 2 functions:

  • function1 without parameters (path_default)
  • function2 with 2 parameters (path_param_0, path_param_1)

Result:

  • function1 (Passed)
  • function2['path_param_0'] (Failed)
    function2 gets the ids of the first parameter, however it receives the default value.
    The fixture is not called for the first parameter, the 'slot' seems to be already used by the default value.
  • function2['path_param_1'] (Passed)

But, if your reverse the call order, every test passes.

  • function2 with 2 parameters (path_param_0, path_param_1)
  • function1 without parameters (default value)

Code:

import pytest

@pytest.fixture(scope='class')
def menunode_path(request):
    result = getattr(request, 'param', None)
    if result is not None:
        assert(result in ('path_param_0', 'path_param_1'))
    else:
        result = 'path_default'
    return result

class TestFailed(object):

    def test_path(self, menunode_path):
        assert(menunode_path == 'path_default')

    @pytest.mark.parametrize('menunode_path', ('path_param_0', 'path_param_1'), 
                             indirect=True, scope='class')
    def test_path_param(self, menunode_path):
        assert(menunode_path in ('path_param_0', 'path_param_1'))

class TestPassed(object):

    @pytest.mark.parametrize('menunode_path', ('path_param_0', 'path_param_1'), 
                             indirect=True, scope='class')
    def test_path_param(self, menunode_path):
        assert(menunode_path in ('path_param_0', 'path_param_1'))

    def test_path(self, menunode_path):
        assert(menunode_path == 'path_default')

Result:

========================== test session starts ===========================
platform linux -- Python 3.6.8, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 -- /home/laurent/Documents/Development/bboss/kconfigrobot/.venv/bin/python
cachedir: .pytest_cache
rootdir: /home/laurent/Documents/Development/bboss/kconfigrobot
collected 6 items                                                        

test_bugg.py::TestFailed::test_path PASSED                         [ 16%]
test_bugg.py::TestFailed::test_path_param[path_param_0] FAILED     [ 33%]
test_bugg.py::TestFailed::test_path_param[path_param_1] PASSED     [ 50%]
test_bugg.py::TestPassed::test_path_param[path_param_0] PASSED     [ 66%]
test_bugg.py::TestPassed::test_path_param[path_param_1] PASSED     [ 83%]
test_bugg.py::TestPassed::test_path PASSED                         [100%]

================================ FAILURES ================================
________________ TestFailed.test_path_param[path_param_0] ________________

self = <test_bugg.TestFailed object at 0x7f5337e96630>
menunode_path = 'path_default'

    @pytest.mark.parametrize('menunode_path', ('path_param_0', 'path_param_1'),
                             indirect=True, scope='class')
    def test_path_param(self, menunode_path):
>       assert(menunode_path in ('path_param_0', 'path_param_1'))
E       AssertionError: assert 'path_default' in ('path_param_0', 'path_param_1')

test_bugg.py:24: AssertionError
=================== 1 failed, 5 passed in 0.05 seconds ===================

System info:
Linux4.15.0-55-generic #60-Ubuntu SMP Tue Jul 2 18:22:20 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

anytree==2.6.0
atomicwrites==1.3.0
attrs==19.1.0
bleach==3.1.0
bumpversion==0.5.3
certifi==2019.6.16
chardet==3.0.4
Cython==0.29.12
docopt==0.6.2
docutils==0.15.1
idna==2.8
importlib-metadata==0.18
kconfiglib==12.12.1
more-itertools==7.2.0
packaging==19.0
pexpect==4.7.0
pkginfo==1.5.0.1
pluggy==0.12.0
ptyprocess==0.6.0
py==1.8.0
pydevd==1.6.1
Pygments==2.4.2
pyparsing==2.4.2a1
pytest==5.0.1
readme-renderer==24.0
requests==2.22.0
requests-toolbelt==0.9.1
robotframework==3.1.2
six==1.12.0
tqdm==4.32.2
twine==1.13.0
urllib3==1.25.3
wcwidth==0.1.7
webencodings==0.5.1
zipp==0.5.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidtopic: fixturesanything involving fixtures directly or indirectlytype: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions