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

Finer grain fixture scope than function #9699

Open
mattharrison opened this issue Feb 19, 2022 · 4 comments
Open

Finer grain fixture scope than function #9699

mattharrison opened this issue Feb 19, 2022 · 4 comments
Labels
topic: fixtures anything involving fixtures directly or indirectly type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@mattharrison
Copy link

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.

@RonnyPfannschmidt
Copy link
Member

there have been proposals to have a "per usage site" scope for a certain types of fixtures as part of multi scope fixtures

currently the fixture system needs a major internal refactoring to support those use-cases

@mattharrison mattharrison changed the title Finer grain fixture score than function Finer grain fixture scope than function Feb 19, 2022
@mattharrison
Copy link
Author

Thanks @RonnyPfannschmidt . 🙏
Would you suggest using a function instead of a fixture in this case?

@RonnyPfannschmidt
Copy link
Member

without more understanding your se-case, the pattern i would suggest to start with is a factory fixture (similar to tmp_path vs tmp_path_factory)

@Zac-HD Zac-HD added topic: fixtures anything involving fixtures directly or indirectly type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature labels Feb 20, 2022
@Zac-HD
Copy link
Member

Zac-HD commented Feb 20, 2022

There are analogous issues with Hypothesis, where Pytest provides a function-scoped fixture only once but the wrapped test is internally invoked many times.

Our usual advice for Hypothesis users is to use context managers rather than fixtures if you need finer-grained resets, and either that or Ronny's suggestion of factory fixtures should work here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: fixtures anything involving fixtures directly or indirectly type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

3 participants