@@ -871,6 +871,115 @@ def test_func(resource):
871871 result = pytester .runpytest ()
872872 result .stdout .fnmatch_lines (["* 2 passed in *" ])
873873
874+ def test_getfixturevalue_teardown_previously_requested_does_not_warn (
875+ self , pytester : Pytester
876+ ) -> None :
877+ """Test that requesting a fixture during teardown that was previously
878+ requested is OK (#12882).
879+
880+ Note: this is still kinda dubious so don't let this test lock you in to
881+ allowing this behavior forever...
882+ """
883+ pytester .makepyfile (
884+ """
885+ import pytest
886+
887+ @pytest.fixture
888+ def fix(request, tmp_path):
889+ yield
890+ assert request.getfixturevalue("tmp_path") == tmp_path
891+
892+ def test_it(fix):
893+ pass
894+ """
895+ )
896+ result = pytester .runpytest ("-Werror" )
897+ result .assert_outcomes (passed = 1 )
898+
899+ def test_getfixturevalue_teardown_new_fixture_deprecated (
900+ self , pytester : Pytester
901+ ) -> None :
902+ """Test that requesting a fixture during teardown that was not
903+ previously requested raises a deprecation warning (#12882).
904+
905+ Note: this is a case that previously worked but will become a hard
906+ error after the deprecation is completed.
907+ """
908+ pytester .makepyfile (
909+ """
910+ import pytest
911+
912+ @pytest.fixture(scope="session")
913+ def resource():
914+ return "value"
915+
916+ @pytest.fixture
917+ def fix(request):
918+ yield
919+ with pytest.warns(
920+ pytest.PytestRemovedIn10Warning,
921+ match=r'Calling request\\ .getfixturevalue\\ ("resource"\\ ) during teardown is deprecated',
922+ ):
923+ assert request.getfixturevalue("resource") == "value"
924+
925+ def test_it(fix):
926+ pass
927+ """
928+ )
929+ result = pytester .runpytest ()
930+ result .assert_outcomes (passed = 1 )
931+
932+ def test_getfixturevalue_teardown_new_inactive_fixture_errors (
933+ self , pytester : Pytester
934+ ) -> None :
935+ """Test that requesting a fixture during teardown that was not
936+ previously requested raises an error (#12882)."""
937+ pytester .makepyfile (
938+ """
939+ import pytest
940+
941+ @pytest.fixture
942+ def fix(request):
943+ yield
944+ request.getfixturevalue("tmp_path")
945+
946+ def test_it(fix):
947+ pass
948+ """
949+ )
950+ result = pytester .runpytest ()
951+ result .assert_outcomes (passed = 1 , errors = 1 )
952+ result .stdout .fnmatch_lines (
953+ [
954+ (
955+ '*The fixture value for "tmp_path" is not available during '
956+ "teardown because it was not previously requested.*"
957+ ),
958+ ]
959+ )
960+
961+ def test_getfixturevalue_teardown_new_inactive_fixture_errors_top_request (
962+ self , pytester : Pytester
963+ ) -> None :
964+ """Test that requesting a fixture during teardown that was not
965+ previously requested raises an error (tricky case) (#12882)."""
966+ pytester .makepyfile (
967+ """
968+ def test_it(request):
969+ request.addfinalizer(lambda: request.getfixturevalue("tmp_path"))
970+ """
971+ )
972+ result = pytester .runpytest ()
973+ result .assert_outcomes (passed = 1 , errors = 1 )
974+ result .stdout .fnmatch_lines (
975+ [
976+ (
977+ '*The fixture value for "tmp_path" is not available during '
978+ "teardown because it was not previously requested.*"
979+ ),
980+ ]
981+ )
982+
874983 def test_getfixturevalue (self , pytester : Pytester ) -> None :
875984 item = pytester .getitem (
876985 """
0 commit comments