Skip to content

Fixture scope is non-deterministic #635

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

Closed
pytestbot opened this issue Nov 27, 2014 · 4 comments
Closed

Fixture scope is non-deterministic #635

pytestbot opened this issue Nov 27, 2014 · 4 comments
Labels
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)


Consider the attached test case. Both fixtures are defined with module scope, so they should be called just once for each parameter value.

However, sometimes the "bar" fixture is called twice for the same parameter value. Example:

#!

$ py.test-3 test_bug.py -s
============================= test session starts =============================
platform linux -- Python 3.4.2 -- py-1.4.25 -- pytest-2.6.3
plugins: capturelog, ordering
collected 4 items 

test_bug.py preparing bar-1
preparing bar-1/foo-1
.preparing bar-2
preparing bar-2/foo-1
.preparing bar-2/foo-2
.preparing bar-1
preparing bar-1/foo-2
.

========================== 4 passed in 0.01 seconds ===========================

$ py.test-3 test_bug.py -s
============================= test session starts =============================
platform linux -- Python 3.4.2 -- py-1.4.25 -- pytest-2.6.3
plugins: capturelog, ordering
collected 4 items 

test_bug.py preparing bar-1
preparing bar-1/foo-1
.preparing bar-1/foo-2
.preparing bar-2
preparing bar-2/foo-2
.preparing bar-2/foo-1
.

========================== 4 passed in 0.01 seconds ===========================

I think the number of calls to a fixture should be determinstic and reproducible.

Moreover, I think the second run (where "preparing bar-1" is printed only once) is correct.


@pytestbot
Copy link
Contributor Author

Original comment by Bruno Oliveira (BitBucket: nicoddemus, GitHub: nicoddemus):


Did you forget to attach test_bug.py?

@pytestbot
Copy link
Contributor Author

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


Indeed I did, here it is.

@pytestbot pytestbot added the type: bug problem that needs to be addressed label Jun 15, 2015
@pfctdayelise
Copy link
Contributor

Here is what was in the test file

#!/usr/bin/env python3
import pytest

@pytest.fixture()
def foo(request, bar):
    s = '%s/foo-%d' % (bar, request.param)
    print('preparing', s)
    return s

@pytest.fixture()
def bar(request):
    s = 'bar-%d' % request.param
    print('preparing', s)
    return s

def test_one(foo):
    pass

def pytest_generate_tests(metafunc):
    foo_params = (1,2)
    bar_params = (1,2)
    if not 'foo' in metafunc.fixturenames:
        return
    assert 'bar' in metafunc.fixturenames

    metafunc.parametrize("foo", foo_params, indirect=True, scope='module')
    metafunc.parametrize("bar", bar_params, indirect=True, scope='module')

@nicoddemus
Copy link
Member

I believe this has been fixed as I can no longer reproduce the problem. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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