Skip to main content

doctortest

doctortest is a simple python package with helpful functionality for writing docstring-based tests.

Less boilerplate for running doctests

Successful example:

def some_function(value: int) -> int:
    """
    Doubles given `value` by 2.

    >>> some_function(0)
    0
    >>> some_function(1)
    2
    >>> some_function(314)
    628
    """
    return value * 2

class SomeModuleTests(unittest.TestCase):
    def test_some_function(self):
        run_doctests(some_function)

An example that fails with AssertionError in case at least one assertion is failing:

def some_function(value: int) -> int:
    """
    Doubles given `value` by 2.
    >>> some_function(0)
    -1
    """
    return value * 2

class SomeModuleTests(unittest.TestCase):
    with self.assertRaises(AssertionError):
        run_doctests(some_function)

Exposing context from other docstrings

Docstrings might use variables defined in other scopes, as shown by the following example:

class Colossus:
    """
    Hello, I am Colossus, the doctor's dog.

    >>> colossus_the_first = Colossus(endurance=3, is_happy=True)
    """

    def __init__(self, endurance: int, is_happy: bool = False) -> None:
        """
        Colossus can be awakened with the following preconditions:

        >>> happy_colossus = Colossus(endurance=8, is_happy=True)
        >>> unhappy_colossus = Colossus(endurance=2)
        """
        self.endurance = endurance
        self.is_happy = is_happy

    def bark(self) -> str:
        """
        Barking as loud as the constructor parameters told me to.

        >>> colossus_the_first.bark()
        'WOOOF'
        >>> happy_colossus.bark()
        'WOOOOOOOOF'
        >>> unhappy_colossus.bark()
        'woof :('
        """
        noise = "W" + "O" * self.endurance + "F"
        if not self.is_happy:
            return noise.lower() + " :("
        return noise

Running the doctests as follows would not work because it would result in NameError: 'unhappy_colossus' is not defined:

class ColossusTests(TestCase):
    def test_bark(self):
        run_doctests(Colossus.bark, out=write_output)

But the following example using parse_docstring_variables would work:

class ColossusTests(TestCase):
    def test_bark(self) -> None:
        run_doctests(
            Colossus.bark,
            globs={
                **parse_docstring_variables(Colossus),
                **parse_docstring_variables(
                    Colossus.__init__,
                    globs={"Colossus": Colossus},
                ),
            },
        )

Metadata

Release files for doctortest 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for doctortest 1.0.1
File Size Uploaded Provenance Actions
doctortest-1.0.1.tar.gz 4.7 kB With provenance Details for doctortest-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for doctortest 1.0.1
File Interpreter ABI Platform Actions
doctortest-1.0.1-py3-none-any.whl Python 3 none any Details for doctortest-1.0.1-py3-none-any.whl

Total release size: 9.3 kB

Release files / doctortest-1.0.1.tar.gz

Download URL doctortest-1.0.1.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
bba506177d08ca1d8db8b09202b08160fd151afb7020f305ddea87f3da713d46
BLAKE2b-256 checksum
How to use checksums
d04bd6e40403ae0b3437bccc787db568c8c53aad88ad8181b1cc215492b937b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 8, 2025.

Transparency log

Release files / doctortest-1.0.1-py3-none-any.whl

Download URL doctortest-1.0.1-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
38700cfaedd385258c4c27727ec7e7df885f0757de72ff79f12c808ca439f315
BLAKE2b-256 checksum
How to use checksums
00023b8d4f07da6ee09af43167c9472f68321974a50e2a64cc3700373f2fb87b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 8, 2025.

Transparency log

Security for doctortest 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for doctortest-1.0.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 8, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page