Sybil

10.0.1 · active · verified Wed Apr 15

Sybil is a Python library designed for automated testing of examples embedded within your code and documentation. It parses examples from various source formats (like reStructuredText, Markdown, and Python docstrings) and evaluates them as part of your standard test suite, integrating with popular test runners such as pytest and unittest. The library is actively maintained, with frequent releases, and is currently at version 10.0.1.

Warnings

Install

Imports

Quickstart

To integrate Sybil with pytest, create a `conftest.py` file in your documentation's root directory. This example configures Sybil to collect doctests and Python code blocks from `.rst` files, and demonstrates how to provide pytest fixtures to the document's namespace. Remember to disable pytest's native doctest plugin in `pytest.ini` or `pyproject.toml`.

import pytest
from os import chdir, getcwd
from shutil import rmtree
from tempfile import mkdtemp

from sybil import Sybil
from sybil.parsers.codeblock import PythonCodeBlockParser
from sybil.parsers.doctest import DocTestParser

@pytest.fixture(scope="module")
def tempdir():
    # Example: Create and clean up a temporary directory for tests
    path = mkdtemp()
    cwd = getcwd()
    try:
        chdir(path)
        yield path
    finally:
        chdir(cwd)
        rmtree(path)

pytest_collect_file = Sybil(
    parsers=[
        DocTestParser(),
        PythonCodeBlockParser(future_imports=['print_function']),
    ],
    pattern='*.rst', # Adjust pattern to match your documentation files
    fixtures=['tempdir'],
).pytest()

view raw JSON →