Skip to content

Automatic grouping of tests by fixture instances broken when using metafunc.parametrize #661

Open
@pytestbot

Description

@pytestbot

Originally reported by: BitBucket: davidkr, GitHub: davidkr


from
Automatic grouping of tests by fixture instances

pytest minimizes the number of active fixtures during test runs. If you have a parametrized
fixture, then all the tests using it will first execute with one instance and then finalizers are
called before the next fixture instance is created. Among other things, this eases testing of
applications which create and use global state.

However the grouping doesn't seem to be working when using metafunc.parametrize. Below is an example of what I mean. Here's the test code...

#!python
import pytest

@pytest.yield_fixture(scope="session",
                      params=["big mac", "whopper"])
def burger(request):
    yield request.param

@pytest.yield_fixture(scope="function",
                      params=["curlyfries", "regularfries"])
def fries(request):
    yield request.param

def test_burgers(burger):
    print "test_burgers: {0}".format(burger)

def test_combo1(burger, fries):
    print("test_combo1: {0}{1}".format(burger, fries))

The output from running the tests..

test_grouping.py::test_burgers[big mac] PASSED
test_grouping.py::test_combo1[big mac-curlyfries] PASSED
test_grouping.py::test_combo1[big mac-regularfries] PASSED
test_grouping.py::test_burgers[whopper] PASSED
test_grouping.py::test_combo1[whopper-curlyfries] PASSED
test_grouping.py::test_combo1[whopper-regularfries] PASSED

So the things get group as I would expect. However I use metafunc to parametrize the burger fixture instead of the decorator by doing this...

#!python
def pytest_generate_tests(metafunc):
    if "burger" in metafunc.fixturenames:
        metafunc.parametrize("burger", argvalues=["big mac", "whopper"])

..then the order changes and all instances of test_burger get run first and the output looks like this....

test_grouping.py::test_burgers[big mac] PASSED
test_grouping.py::test_burgers[whopper] PASSED
test_grouping.py::test_combo1[big mac-curlyfries] PASSED
test_grouping.py::test_combo1[big mac-regularfries] PASSED
test_grouping.py::test_combo1[whopper-curlyfries] PASSED
test_grouping.py::test_combo1[whopper-regularfries] PASSED

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytopic: parametrizerelated to @pytest.mark.parametrize

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions