pytest-dev / pytest-dev/pytest

Recursive parameterized fixture bug

Open
#6,416 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

When overriding session scoped fixtures with a parameterized fixture that is used as the input to another fixture the recursive fixture is not properly invoked.

I have created a minimal reproduction here with more information: https://github.com/baszalmstra/pytest_fixture_bug

Minimal example

Given the files:

conftest.py

import pytest


@pytest.fixture(scope="session")
def parameterized_fixture(request):
    return request.param


@pytest.fixture(scope="session")
def recursive_fixture(parameterized_fixture):
    print("Calling recursive_fixture with: {}".format(parameterized_fixture))
    return parameterized_fixture

test_bar.py

import pytest


@pytest.fixture(scope="session", params=["bar1"])
def parameterized_fixture(request):
    print("Calling parameterized_fixture with: {}".format(request.param))
    return request.param


def test_bar(recursive_fixture):
    print("Calling test_foo with: {}".format(recursive_fixture))
    assert("bar" in recursive_fixture)

test_foo.py

import pytest


@pytest.fixture(scope="session", params=["foo1", "foo2"])
def parameterized_fixture(request):
    print("Calling parameterized_fixture with: {}".format(request.param))
    return request.param


def test_foo(recursive_fixture):
    print("Calling test_bar with: {}".format(recursive_fixture))
    assert("foo" in recursive_fixture)

Calling pytest fails with:

________________________________ test_foo[foo1] ________________________________

recursive_fixture = 'bar1'

    def test_foo(recursive_fixture):
        print("Calling test_bar with: {}".format(recursive_fixture))
>       assert("foo" in recursive_fixture)
E       AssertionError: assert 'foo' in 'bar1'

test_foo.py:12: AssertionError
---------------------------- Captured stdout setup -----------------------------
Calling parameterized_fixture with: foo1
----------------------------- Captured stdout call -----------------------------
Calling test_bar with: bar1

Which should not be possible.

We have observed this issue with both pytest-4.6.9 as well as with pytest-5.3.2 on Ubuntu 18.04. We are still on python 2.7 so a backported solution to pytest 4 would be desirable.

This seems to be related to: #5693

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 by reproducing the failure with the conftest.py, test_bar.py, and test_foo.py examples in the issue, then compare the behavior with related issue #5693. Trace how the session-scoped recursive_fixture resolves the overridden parameterized_fixture. Done means each test receives its own parameter value instead of the value from another module, with coverage for the reported Python 2.7-compatible behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.