Skip to main content

Golem Documentation

Michael Bernstein

Doctest & Code Example Verification

Verifying Executable Code Examples

Technical documentation easily becomes stale when APIs change. Golem integrates with asciidoctest to parse and execute Python code blocks embedded within AsciiDoc documents.

Run doctest verification across your documentation:

Terminal
BASH
golem doctest

Per-Section Test Isolation (--split-sections)

By default, golem doctest executes all test blocks within a document in a single runner session. When any test block encounters a failure, test execution for that document halts.

Pass the --split-sections flag to partition test blocks by document section (== headings) and execute each section in its own isolated execution environment:

Terminal
BASH
golem doctest --split-sections

What It Does

When --split-sections is enabled:

  • Each section defined by a section heading (==) receives a dedicated namespace and execution context.
  • A test failure in one section does not abort test execution for subsequent sections in the same document.
  • Test pass/fail counts and diagnostics are aggregated and reported per section.

When to Use It

  • Comprehensive test runs: When you want full visibility into test status across an entire document rather than halting on the first failure in an early section.
  • Independent document chapters and tutorials: In guides where each section represents a self-contained tutorial or workflow with its own variables and setup.
  • State isolation: To guarantee that variables, monkey-patching, or environment modifications from one section do not leak into downstream sections.

Usage Examples

Run doctest verification with section splitting across all documents:

Terminal
BASH
golem doctest --split-sections

Combine --split-sections with a specific file path and verbose output:

Terminal
BASH
golem doctest --split-sections -v docs/reference/doctest.adoc

Authoring Executable Doctest Blocks

In your AsciiDoc source:

config_doctest.adoc
ASCIIDOC
.config_example.py
[source,python,role="test"]
....
>>> from golem.config import GolemConfig
>>> config = GolemConfig(content_dir="docs")
>>> config.content_dir
'docs'
....
config_example.py
>>> from golem.config import GolemConfig
>>> config = GolemConfig(content_dir="docs")
>>> config.content_dir
'docs'

Running golem doctest extracts these blocks and executes them in an isolated test environment. In the rendered HTML documentation, the block automatically receives a TEST role badge and a PYTHON language badge.

Test Block Roles & State Models

Golem's doctest plugin supports advanced state management and isolation models:

Isolated Blocks (Default)

By default or when marked with [source,python,role="test"], blocks run in isolated, ephemeral state:

isolated_test.adoc
ASCIIDOC
.calculator_test.py
[source,python,role="test"]
....
>>> a = 10
>>> a * 2
20
....
calculator_test.py
>>> a = 10
>>> a * 2
20

Shared State Timelines

Use role="shared" to retain variable definitions and state across subsequent blocks:

shared_timeline.adoc
ASCIIDOC
.api_setup.py
[source,python,role="shared"]
....
base_url = "https://example.com"
....

.api_test.py
[source,python,role="shared test"]
....
>>> f"{base_url}/api"
'https://example.com/api'
....
api_setup.py
base_url = "https://example.com"
api_test.py
>>> f"{base_url}/api"
'https://example.com/api'

Section Boundary Scoping

Shared state automatically resets at top-level section boundaries (==), isolating independent document chapters or topics while preserving sequential state flow within each section.

When running doctests with the --split-sections CLI flag, each section is additionally executed in its own isolated runner invocation, so a failure in one section does not abort test execution for subsequent sections.

Named Context Scopes

Manage multiple independent timelines within a single document using named contexts:

named_scopes.adoc
ASCIIDOC
.server_config.py
[source,python,role="shared",shared="server"]
....
port = 8080
....

.client_config.py
[source,python,role="shared",shared="client"]
....
timeout = 30
....
server_config.py
port = 8080
client_config.py
timeout = 30

Explicit Reset Markers

Clear all accumulated shared state and named contexts on demand using role="reset":

reset_state.adoc
ASCIIDOC
.clean_slate.py
[source,python,role="reset test"]
....
>>> "base_url" in locals() or "base_url" in globals()
False
....
clean_slate.py
>>> "base_url" in locals() or "base_url" in globals()
False