Skip to main content

Utilities for writing Python doctests

Project description

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},
                ),
            },
        )

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

doctortest-1.0.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

doctortest-1.0.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file doctortest-1.0.1.tar.gz.

File metadata

  • Download URL: doctortest-1.0.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for doctortest-1.0.1.tar.gz
Algorithm Hash digest
SHA256 bba506177d08ca1d8db8b09202b08160fd151afb7020f305ddea87f3da713d46
MD5 4810f6615cb96f50f2ec5dc3bb39242c
BLAKE2b-256 d04bd6e40403ae0b3437bccc787db568c8c53aad88ad8181b1cc215492b937b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for doctortest-1.0.1.tar.gz:

Publisher: python-publish.yml on puzzleYOU/doctortest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file doctortest-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: doctortest-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for doctortest-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 38700cfaedd385258c4c27727ec7e7df885f0757de72ff79f12c808ca439f315
MD5 75eb456a1aa1c75872b8f28ce40b115c
BLAKE2b-256 00023b8d4f07da6ee09af43167c9472f68321974a50e2a64cc3700373f2fb87b

See more details on using hashes here.

Provenance

The following attestation bundles were made for doctortest-1.0.1-py3-none-any.whl:

Publisher: python-publish.yml on puzzleYOU/doctortest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page