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

metafunc.parametrize overwrites scope #634

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

metafunc.parametrize overwrites scope #634

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)


According to http://pytest.org/latest/parametrize.html#the-metafunc-object, the scope parameter overrides the fixture scope if specified. But in practice, the fixture scope seems to be ignored even if no scope parameter is passed to metafunc.parametrize. Example:

#!python

$ cat test_me.py 
#!/usr/bin/env python3

import pytest
import logging

log = logging.getLogger(__name__)

@pytest.fixture(scope='module')
def foo(request):
    print('preparing foo-%d' % request.param)
    return 'foo-%d' % request.param

def test_one(foo):
    print(foo)

def test_two(foo):
    print(foo)
test_two.test_with = (2,3)

def pytest_generate_tests(metafunc):
    params = list(range(4))
    if not 'foo' in metafunc.fixturenames:
        return

    test_with =  getattr(metafunc.function, 'test_with', None)
    if test_with:
        params = test_with
    metafunc.parametrize("foo", params, indirect=True)

$ py.test-3 test_me.py -v -s
============================= test session starts =============================
platform linux -- Python 3.4.2 -- py-1.4.25 -- pytest-2.6.3 -- /usr/bin/python3
plugins: capturelog, ordering
collected 6 items 

test_me.py::test_one[0] preparing foo-0
foo-0
PASSED
test_me.py::test_one[1] preparing foo-1
foo-1
PASSED
test_me.py::test_one[2] preparing foo-2
foo-2
PASSED
test_me.py::test_one[3] preparing foo-3
foo-3
PASSED
test_me.py::test_two[2] preparing foo-2
foo-2
PASSED
test_me.py::test_two[3] preparing foo-3
foo-3
PASSED

========================== 6 passed in 0.01 seconds ===========================

Note how the fixture is called multiple times for the same parameter.


@pytestbot
Copy link
Contributor Author

Original comment by Thomas Brezinski (BitBucket: tbrezins, GitHub: tbrezins):


I believe this is the same issue I'm running into when trying to create class-scoped fixtures and method-level fixtures. The class scoped fixture is being created per test rather than once per fixture value.

@pytestbot
Copy link
Contributor Author

Original comment by Anatoly Bubenkov (BitBucket: bubenkoff, GitHub: bubenkoff):


did you try the latest code from the repo, not the latest release, still the same issue?

@pytestbot
Copy link
Contributor Author

Original comment by Thomas Brezinski (BitBucket: tbrezins, GitHub: tbrezins):


Yes, the issue seems to be present in the latest code.

@pytestbot
Copy link
Contributor Author

Original comment by BitBucket: zmastervader, GitHub: zmastervader:


I have run into this exact same issue in the release of 2.7. Is there any ETA on a fix?

@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
This was referenced Nov 11, 2017
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

2 participants