tests.sphinx.conftest moduleΒΆ

Fixtures and helpers for Sphinx tests.

Separated from the root tests/conftest.py so that the Sphinx dependency (and its ecosystem: myst-parser, docutils, etc.) is only imported when running the tests in this subdirectory.

class tests.sphinx.conftest.FormatType(*values)[source]ΒΆ

Bases: Enum

Sphinx document format types and their file extensions.

RST = '.rst'ΒΆ
MYST = '.md'ΒΆ
class tests.sphinx.conftest.SphinxAppWrapper(app, format_type)[source]ΒΆ

Bases: object

Wrapper around Sphinx application with additional testing methods.

classmethod create(format_type, tmp_path, return_srcdir=False, enable_exec_directives=True)[source]ΒΆ

Factory method to create a SphinxAppWrapper with given format.

enable_exec_directives opts the test app into the click:* and python:* directive families. Defaults to True for every fixture-built app because the test suite primarily exercises those directives. Tests that verify the off-by-default production gate flip the flag back to False explicitly.

Return type:

Generator[SphinxAppWrapper | tuple[SphinxAppWrapper, Path], None, None]

build_document(content)[source]ΒΆ

Build a Sphinx document with content and return the HTML output.

Automatically detects the format from the app configuration and uses the appropriate file extension (.rst or .md).

Return type:

str | None

generate_test_content(test_case)[source]ΒΆ

Generate content for a test case based on the app’s format type.

Return type:

str

tests.sphinx.conftest.sphinx_app(request, tmp_path)[source]ΒΆ

Create a Sphinx application for testing.

tests.sphinx.conftest.sphinx_app_for_format(request, tmp_path)[source]ΒΆ

sphinx_app variant whose format is supplied by the caller.

Identical to sphinx_app() but without the default params: the FormatType is taken from the test’s indirect parametrization. A params-bearing fixture cannot also be parametrized by a test (pytest rejects it as a duplicate parametrization), so a test that drives the format per case parametrizes this variant instead, usually through format_params().

tests.sphinx.conftest.sphinx_app_rst(tmp_path)[source]ΒΆ

Create a Sphinx application for testing RST format only.

The click:* and python:* directive families are enabled so tests can exercise them. Tests that explicitly verify the off-by- default opt-in gate construct their own app via SphinxAppWrapper.create() with enable_exec_directives=False.

tests.sphinx.conftest.sphinx_app_myst(tmp_path)[source]ΒΆ

Create a Sphinx application for testing MyST format only.

The click:* and python:* directive families are enabled: see sphinx_app_rst() for the rationale.

tests.sphinx.conftest.sphinx_app_myst_with_include(tmp_path)[source]ΒΆ

Create a Sphinx application for testing MyST format with include files.

class tests.sphinx.conftest.DirectiveTestCase(name, format_type=None, source_block=None, run_block=None, document=None, html_matches=None)[source]ΒΆ

Bases: object

Test case data for directive tests.

name: strΒΆ
format_type: FormatType | None = NoneΒΆ
source_block: str | None = NoneΒΆ
run_block: str | None = NoneΒΆ
document: str | None = NoneΒΆ
html_matches: Sequence[str] | str | None = NoneΒΆ
tests.sphinx.conftest.format_params(*test_cases)[source]ΒΆ

Pair each test case with every document format it is written for.

Feeds the indirect parametrization of sphinx_app_for_format(), so a case is only ever built by an app whose format it can render: a case pinned to a FormatType yields that format alone, a format-agnostic one yields a parameter per format.

Return type:

tuple[ParameterSet, ...]

tests.sphinx.conftest.unescape_quotes(text)[source]ΒΆ

Replace quote entities with bare quotes, so both escapings compare equal.

Pygments 2.21.0 replaced its escape_html() helper with html.escape(value, quote=False). Token text carries bare " and ' since then, where older releases produced " and '. Both forms render the same, and the pygments extra declares a floor spanning that release, so HTML fragments are compared with quotes normalized.

Return type:

str

tests.sphinx.conftest.plain_whitespace(text)[source]ΒΆ

Replace whitespace tokens with bare spaces, so both lexings compare equal.

Pygments 2.19.0 made the Python lexer emit some blanks of a def or from statement as whitespace tokens, where older releases leave them as bare text. Both forms render the same, and the sphinx extra declares a floor below that release, so HTML fragments are compared with these tokens flattened.

Return type:

str

tests.sphinx.conftest.python_block(*lines)[source]ΒΆ

Build expected Python highlight block.

Return type:

str

tests.sphinx.conftest.shell_block(*lines)[source]ΒΆ

Build expected shell session block.

Return type:

str

tests.sphinx.conftest.admonition_block(admonition_type, content)[source]ΒΆ

Build expected admonition block.

Parameters:
  • admonition_type (str) – The type of admonition (note, tip, warning, etc.)

  • content (str) – The inner HTML content of the admonition (without the title)

Return type:

str