def test_scoped_fixture_caching(pytester: Pytester) -> None:
    """Make sure setup and finalization is only run once when using scoped fixture
    multiple times."""
    pytester.makepyfile(
        """
        from __future__ import annotations

        from typing import Generator

        import pytest
        executed: list[str] = []
        @pytest.fixture(scope="class")
        def fixture_1() -> Generator[None, None, None]:
            executed.append("fix setup")
            yield
            executed.append("fix teardown")


        class TestFixtureCaching:
            def test_1(self, fixture_1: None) -> None:
                assert executed == ["fix setup"]

            def test_2(self, fixture_1: None) -> None:
                assert executed == ["fix setup"]


        def test_expected_setup_and_teardown() -> None:
            assert executed == ["fix setup", "fix teardown"]
        """
    )
    result = pytester.runpytest()
    assert result.ret == 0
Make sure setup and finalization is only run once when using scoped fixture
    multiple times.
testing/python/fixtures.py