pytest-dev / pytest-dev/pytest

Finer grain fixture scope than function

Open
#9,699 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: fixtures type: proposal
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduced test in test_fixturegrain.py and the function-scoped fixture behavior shown in the issue. Read the fixture-scope documentation and relevant pytest tests, then establish how a finer-grained scope should behave when one test references a fixture multiple times; done means the example passes with isolated fixture values and the behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.