Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with fixture parametrization #531

Closed
pytestbot opened this issue Jun 24, 2014 · 6 comments
Closed

Problems with fixture parametrization #531

pytestbot opened this issue Jun 24, 2014 · 6 comments
Labels
topic: fixtures anything involving fixtures directly or indirectly topic: parametrize related to @pytest.mark.parametrize type: bug problem that needs to be addressed

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Nikolaus Rath (BitBucket: nikratio, GitHub: nikratio)


Please consider the attached test. In this form, it runs just fine:

text

$ py.test-3 -v -s test_bug.py 
=========================== test session starts ============================
platform linux -- Python 3.4.1 -- py-1.4.20 -- pytest-2.5.2 -- /usr/bin/python3
collected 3 items 

test_bug.py:31: test_one[val1-globorz] PASSED
test_bug.py:34: test_two[val1-globorz] PASSED
test_bug.py:31: test_one[val2-globorz] PASSED

========================= 3 passed in 0.01 seconds =========================

However, if you reverse the order of the first fixture parametrization, i.e. if you use

values = [ Container('val2'), Container('val1') ]

instead of

values = [ Container('val1'), Container('val2') ]

the test suddenly fails with:

text

test_bug.py:31: test_one[val2-globorz] PASSED
test_bug.py:34: test_two[val1-globorz] FAILED
test_bug.py:31: test_one[val1-globorz] PASSED

================================= FAILURES =================================
__________________________ test_two[val1-globorz] __________________________

fix1 = (<test_bug.Container object at 0x7f9574bf5cc0>, 'globorz')

    def test_two(fix1):
>       assert fix1[0].v == 'val1'
E       assert 'val2' == 'val1'
E         - val2
E         ?    ^
E         + val1
E         ?    ^

test_bug.py:35: AssertionError

It seems that py.test thinks that it is passing "val1" to test_two, but it actually passes "val2".


@pytestbot
Copy link
Contributor Author

Original comment by Nikolaus Rath (BitBucket: nikratio, GitHub: nikratio):


I managed to simply the testcase even further. Note that removing scope='module' from the parametrize call seems to work around the problem.

@pytestbot
Copy link
Contributor Author

Original comment by Floris Bruynooghe (BitBucket: flub, GitHub: flub):


Hi, I can't see the the attached code, did you forget to add it or am I being blind?

@pytestbot
Copy link
Contributor Author

Original comment by Nikolaus Rath (BitBucket: nikratio, GitHub: nikratio):


It's attached for sure. I wouldn't venture a guess on the second part of the question :-).

Look at the bullet point under my name in the first comment.

@pytestbot
Copy link
Contributor Author

Original comment by Nikolaus Rath (BitBucket: nikratio, GitHub: nikratio):


Aeh, hm. Clicking on that link gives a 404 error though. Let me try again...

@pytestbot pytestbot added the type: bug problem that needs to be addressed label Jun 15, 2015
@pfctdayelise pfctdayelise added the topic: parametrize related to @pytest.mark.parametrize label Jul 25, 2015
@pfctdayelise
Copy link
Contributor

test_buy.py:

import pytest

values = [ 'val2', 'val1' ]

def pytest_generate_tests(metafunc):
    if not 'fixture' in metafunc.fixturenames:
        return

    if hasattr(metafunc.function, 'requires_val1'):
        vals = [ x for x in values if x == 'val1' ]
    else:
        vals = values
    metafunc.parametrize("fixture", vals, ids=[ str(v) for v in vals ],
                         scope='module', indirect=True)

@pytest.fixture()
def fixture(request):
    return request.param

def test_one(fixture):
    assert fixture in ('val1', 'val2')

def test_two(fixture):
    assert fixture == 'val1'
test_two.requires_val1 = True

@Zac-HD
Copy link
Member

Zac-HD commented Nov 27, 2019

Closing this issue because I can't get it to fail under current versions of pytest 😄

@Zac-HD Zac-HD closed this as completed Nov 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: fixtures anything involving fixtures directly or indirectly topic: parametrize related to @pytest.mark.parametrize type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

3 participants