Skip to content

Finer grain fixture scope than function #9699

@mattharrison

Description

@mattharrison

What's the problem this feature will solve?

I'm testing some pandas refactoring code. I have an original data frame and old code that creates a new dataframe (actually mutates the original), and new code (that doesn't mutate the original).

I want to use the original data as a fixture for both the old and new code. However, because they both depend on the fixture that is function scoped, it is only created once and the mutation code messes up the test.

Here's an example (using Lists instead of DataFrames):

import pytest

@pytest.fixture
def value_list():
    return [2, 10, 20]

@pytest.fixture
def old_results(value_list):
    return old_code(value_list)

@pytest.fixture
def new_results(value_list):
    return new_code(value_list)

def old_code(seq):
    for i in range(len(seq)):
        seq[i] *= 2
    return seq

def new_code(seq):
    return [2*val for val in seq]

def test_functionality(old_results, new_results):
    assert old_results == new_results

Output:

============================= test session starts ==============================
platform linux -- Python 3.8.5, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: /mnt/c/Users/matt/Dropbox/work/courses/RefactoringPandas
plugins: flake8-1.0.7, annotate-1.0.3, cov-2.11.1, hypothesis-6.8.1, pudb-0.7.0, anyio-3.4.0, mypy-0.8.0, Faker-11.3.0
collected 1 item                                                               

test_fixturegrain.py F                                                   [100%]

=================================== FAILURES ===================================
______________________________ test_functionality ______________________________

old_results = [4, 20, 40], new_results = [8, 40, 80]

    def test_functionality(old_results, new_results):
>       assert old_results == new_results
E       assert [4, 20, 40] == [8, 40, 80]
E         At index 0 diff: 4 != 8
E         Use -v to get the full diff

test_fixturegrain.py:24: AssertionError
=========================== short test summary info ============================
FAILED test_fixturegrain.py::test_functionality - assert [4, 20, 40] == [8, 4...
============================== 1 failed in 0.18s ===============================

Describe the solution you'd like

I would propose a finer grain scope that function. Not sure of the name? every? If a fixture has this score and a single test function references the fixture multiple times then each fixture is a fresh invocation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytype: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions