Sybil
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
- gotcha pytest has its own doctest plugin which can conflict with Sybil. Ensure you disable it in your pytest configuration to avoid unexpected behavior or duplicate test runs.
- gotcha The `path` parameter of the `Sybil` class is ignored when using pytest integration. This can be confusing for users accustomed to its behavior in unittest integration, where it's used to specify the documentation source directory.
- gotcha Incorrectly configuring parsers for your documentation format (e.g., using ReST parsers for Markdown files) will result in examples not being discovered or tested. Sybil provides specific parsers for reStructuredText, Markdown, MyST, and Python code blocks.
- gotcha The name 'sybil' is used by other Python libraries (e.g., 'sypy' for Sybil node detection, 'sybil' for medical imaging AI). Always ensure you are installing and importing `sybil` (from simplistix/sybil on GitHub) for documentation testing to avoid conflicts or incorrect library usage.
Install
-
pip install sybil -
pip install sybil[pytest]
Imports
- Sybil
from sybil import Sybil
- DocTestParser
from sybil.parsers.doctest import DocTestParser
- PythonCodeBlockParser
from sybil.parsers.codeblock import PythonCodeBlockParser
Quickstart
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()