From 396bfbf30b1594b6f56528913d6144826060198e Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Mon, 17 Jul 2023 23:58:35 +0300 Subject: [PATCH] fixtures: add a test for a currently non-covered scope mismatch scenario This test makes clear the need for the `_check_scope()` call in the `pytest_setup_fixture` impl in fixtures.py, which otherwise seems redundant with the one in `_compute_fixture_value`. --- testing/python/fixtures.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index 63c18456a..191689d1c 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -2464,6 +2464,31 @@ class TestFixtureMarker: ["*ScopeMismatch*You tried*function*session*request*"] ) + def test_scope_mismatch_already_computed_dynamic(self, pytester: Pytester) -> None: + pytester.makepyfile( + test_it=""" + import pytest + + @pytest.fixture(scope="function") + def fixfunc(): pass + + @pytest.fixture(scope="module") + def fixmod(fixfunc): pass + + def test_it(request, fixfunc): + request.getfixturevalue("fixmod") + """, + ) + + result = pytester.runpytest() + assert result.ret == ExitCode.TESTS_FAILED + result.stdout.fnmatch_lines( + [ + "*ScopeMismatch*involved factories*", + "test_it.py:6: def fixmod(fixfunc)", + ] + ) + def test_dynamic_scope(self, pytester: Pytester) -> None: pytester.makeconftest( """