Skip to content

Commit 882a126

Browse files
committed
tests(sphinx_pytest_fixtures): add targeted behavior tests for coverage gaps
why: _classify_deps, _build_usage_snippet, and _on_env_purge_doc had no direct unit tests despite being core extension logic. what: - test_classify_deps_project_fixture: non-builtin dep → project list - test_classify_deps_hidden_fixture: pytestconfig → hidden list - test_classify_deps_deprecated_config_merge: old + new config → union - test_build_usage_snippet_resource_returns_none: resource → None - test_build_usage_snippet_autouse_returns_note: autouse → nodes.note - test_env_purge_doc_removes_only_target: selective purge behavior
1 parent 9f0f248 commit 882a126

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

tests/docs/_ext/test_sphinx_pytest_fixtures.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,3 +814,134 @@ def my_fixture() -> str:
814814
)
815815

816816
assert any("custom_weird_kind" in r.message for r in caplog.records)
817+
818+
819+
# ---------------------------------------------------------------------------
820+
# _classify_deps
821+
# ---------------------------------------------------------------------------
822+
823+
824+
def test_classify_deps_project_fixture() -> None:
825+
"""Non-builtin, non-hidden dep is classified as a project fixture."""
826+
827+
@pytest.fixture
828+
def my_fixture(server: t.Any) -> str:
829+
return "hello"
830+
831+
project, builtin, hidden = sphinx_pytest_fixtures._classify_deps(my_fixture, None)
832+
assert "server" in project
833+
assert "server" not in builtin
834+
assert "server" not in hidden
835+
836+
837+
def test_classify_deps_hidden_fixture() -> None:
838+
"""Fixture depending on pytestconfig has it classified as hidden."""
839+
840+
@pytest.fixture
841+
def my_fixture(pytestconfig: t.Any) -> str:
842+
return "hello"
843+
844+
project, _builtin, hidden = sphinx_pytest_fixtures._classify_deps(my_fixture, None)
845+
assert "pytestconfig" in hidden
846+
assert "pytestconfig" not in project
847+
848+
849+
def test_classify_deps_deprecated_config_merge() -> None:
850+
"""Both old and new hidden config are merged (union)."""
851+
852+
@pytest.fixture
853+
def my_fixture(pytestconfig: t.Any, capfd: t.Any, my_dep: t.Any) -> str:
854+
return "hello"
855+
856+
# Old-style config adds "my_dep" to hidden set
857+
app = types.SimpleNamespace(
858+
config=types.SimpleNamespace(
859+
pytest_fixture_hidden_dependencies=frozenset({"pytestconfig"}),
860+
pytest_internal_fixtures=frozenset({"my_dep"}),
861+
pytest_fixture_builtin_links={},
862+
pytest_external_fixture_links={},
863+
),
864+
)
865+
_project, _builtin, hidden = sphinx_pytest_fixtures._classify_deps(my_fixture, app)
866+
assert "pytestconfig" in hidden
867+
assert "my_dep" in hidden
868+
assert "capfd" not in hidden # capfd not in either config set
869+
870+
871+
# ---------------------------------------------------------------------------
872+
# _build_usage_snippet
873+
# ---------------------------------------------------------------------------
874+
875+
876+
def test_build_usage_snippet_resource_returns_none() -> None:
877+
"""Resource fixtures return None (generic snippet suppressed)."""
878+
result = sphinx_pytest_fixtures._build_usage_snippet(
879+
"server", "Server", "resource", "function", autouse=False
880+
)
881+
assert result is None
882+
883+
884+
def test_build_usage_snippet_autouse_returns_note() -> None:
885+
"""Autouse fixtures return a nodes.note admonition."""
886+
from docutils import nodes
887+
888+
result = sphinx_pytest_fixtures._build_usage_snippet(
889+
"auto_cleanup", None, "resource", "function", autouse=True
890+
)
891+
assert isinstance(result, nodes.note)
892+
assert "No request needed" in result.astext()
893+
894+
895+
# ---------------------------------------------------------------------------
896+
# _on_env_purge_doc
897+
# ---------------------------------------------------------------------------
898+
899+
900+
def test_env_purge_doc_removes_only_target() -> None:
901+
"""Purging a doc removes only that doc's fixtures from the store."""
902+
env = types.SimpleNamespace(
903+
domaindata={
904+
"sphinx_pytest_fixtures": {
905+
"fixtures": {
906+
"mod.fixture_a": sphinx_pytest_fixtures.FixtureMeta(
907+
docname="page_a",
908+
canonical_name="mod.fixture_a",
909+
public_name="fixture_a",
910+
source_name="fixture_a",
911+
scope="function",
912+
autouse=False,
913+
kind="resource",
914+
return_display="str",
915+
return_xref_target=None,
916+
deps=(),
917+
param_reprs=(),
918+
has_teardown=False,
919+
is_async=False,
920+
),
921+
"mod.fixture_b": sphinx_pytest_fixtures.FixtureMeta(
922+
docname="page_b",
923+
canonical_name="mod.fixture_b",
924+
public_name="fixture_b",
925+
source_name="fixture_b",
926+
scope="function",
927+
autouse=False,
928+
kind="resource",
929+
return_display="str",
930+
return_xref_target=None,
931+
deps=(),
932+
param_reprs=(),
933+
has_teardown=False,
934+
is_async=False,
935+
),
936+
},
937+
"public_to_canon": {},
938+
"reverse_deps": {},
939+
"_store_version": sphinx_pytest_fixtures._STORE_VERSION,
940+
},
941+
},
942+
)
943+
app = types.SimpleNamespace()
944+
sphinx_pytest_fixtures._on_env_purge_doc(app, env, "page_a")
945+
store = env.domaindata["sphinx_pytest_fixtures"]
946+
assert "mod.fixture_a" not in store["fixtures"]
947+
assert "mod.fixture_b" in store["fixtures"]

0 commit comments

Comments
 (0)