@@ -820,3 +820,97 @@ def test_foo(mocker):
820820 py_fn .remove ()
821821 result = testdir .runpytest ()
822822 result .stdout .fnmatch_lines ("* 1 passed *" )
823+
824+
825+ def test_used_with_class_scope (testdir ):
826+ """..."""
827+ testdir .makepyfile (
828+ """
829+ import pytest
830+ import random
831+ import unittest
832+
833+ def get_random_number():
834+ return random.randint(0, 1)
835+
836+ @pytest.fixture(autouse=True, scope="class")
837+ def randint_mock(class_mocker):
838+ return class_mocker.patch("random.randint", lambda x, y: 5)
839+
840+ class TestGetRandomNumber(unittest.TestCase):
841+ def test_get_random_number(self):
842+ assert get_random_number() == 5
843+ """
844+ )
845+ result = testdir .runpytest_subprocess ()
846+ assert "AssertionError" not in result .stderr .str ()
847+ result .stdout .fnmatch_lines ("* 1 passed in *" )
848+
849+
850+ def test_used_with_module_scope (testdir ):
851+ """..."""
852+ testdir .makepyfile (
853+ """
854+ import pytest
855+ import random
856+
857+ def get_random_number():
858+ return random.randint(0, 1)
859+
860+ @pytest.fixture(autouse=True, scope="module")
861+ def randint_mock(module_mocker):
862+ return module_mocker.patch("random.randint", lambda x, y: 5)
863+
864+ def test_get_random_number():
865+ assert get_random_number() == 5
866+ """
867+ )
868+ result = testdir .runpytest_subprocess ()
869+ assert "AssertionError" not in result .stderr .str ()
870+ result .stdout .fnmatch_lines ("* 1 passed in *" )
871+
872+
873+ def test_used_with_package_scope (testdir ):
874+ """..."""
875+ testdir .makepyfile (
876+ """
877+ import pytest
878+ import random
879+
880+ def get_random_number():
881+ return random.randint(0, 1)
882+
883+ @pytest.fixture(autouse=True, scope="package")
884+ def randint_mock(package_mocker):
885+ return package_mocker.patch("random.randint", lambda x, y: 5)
886+
887+ def test_get_random_number():
888+ assert get_random_number() == 5
889+ """
890+ )
891+ result = testdir .runpytest_subprocess ()
892+ assert "AssertionError" not in result .stderr .str ()
893+ result .stdout .fnmatch_lines ("* 1 passed in *" )
894+
895+
896+ def test_used_with_session_scope (testdir ):
897+ """..."""
898+ testdir .makepyfile (
899+ """
900+ import pytest
901+ import random
902+
903+ def get_random_number():
904+ return random.randint(0, 1)
905+
906+ @pytest.fixture(autouse=True, scope="session")
907+ def randint_mock(session_mocker):
908+ return session_mocker.patch("random.randint", lambda x, y: 5)
909+
910+ def test_get_random_number():
911+ assert get_random_number() == 5
912+ """
913+ )
914+ result = testdir .runpytest_subprocess ()
915+ assert "AssertionError" not in result .stderr .str ()
916+ result .stdout .fnmatch_lines ("* 1 passed in *" )
0 commit comments