Skip to content

fixture scope is ignored when using metafunc.parametrize() #519

Closed
@pytestbot

Description

@pytestbot

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


Please take a look at the following example:

import pytest

def pytest_generate_tests(metafunc):
    if 'arg1' in metafunc.fixturenames:
        metafunc.parametrize("arg1", [ 'arg1v1', 'arg1v2' ], scope='module')

    if 'arg2' in metafunc.fixturenames:
        metafunc.parametrize("arg2", [ 'arg2v1', 'arg2v2' ], scope='function')

@pytest.yield_fixture(scope='module')
def fix1(arg1):
    print('running fix1 with', arg1)
    yield 'fix1-' + arg1

@pytest.yield_fixture(scope='function')
def fix2(fix1, arg2):
    print('running fix2 with', fix1, arg2)
    yield 'fix2-' + arg2 + fix1

def test_one(fix2):
    assert True

def test_two(fix2):
    assert True

As I understand, test_one and test_two should be executed 4 times each. The fix2 function should be called once for every test execution (because it has function scope), and the fix1 function once for every value of arg1 (because it has module scope).

However, in practice fix1 seems to be called for every test function as well, as if it had function scope:

$ python3 ~/clones/pytest/pytest.py -v -s test_bug.py
============================= test session starts ==============================
platform linux -- Python 3.3.5 -- py-1.4.20 -- pytest-2.6.0.dev1 -- /usr/bin/python3
collected 8 items 

test_bug.py@20::test_one[arg1v1-arg2v1] running fix1 with arg1v1
running fix2 with fix1-arg1v1 arg2v1
PASSED
test_bug.py@23::test_two[arg1v1-arg2v1] running fix2 with fix1-arg1v1 arg2v1
PASSED
test_bug.py@20::test_one[arg1v1-arg2v2] running fix1 with arg1v1
running fix2 with fix1-arg1v1 arg2v2
PASSED
test_bug.py@23::test_two[arg1v1-arg2v2] running fix2 with fix1-arg1v1 arg2v2
PASSED
test_bug.py@20::test_one[arg1v2-arg2v1] running fix1 with arg1v2
running fix2 with fix1-arg1v2 arg2v1
PASSED
test_bug.py@23::test_two[arg1v2-arg2v1] running fix2 with fix1-arg1v2 arg2v1
PASSED
test_bug.py@20::test_one[arg1v2-arg2v2] running fix1 with arg1v2
running fix2 with fix1-arg1v2 arg2v2
PASSED
test_bug.py@23::test_two[arg1v2-arg2v2] running fix2 with fix1-arg1v2 arg2v2
PASSED

=========================== 8 passed in 0.01 seconds ===========================

Looks like a bug to me.

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