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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bba506177d08ca1d8db8b09202b08160fd151afb7020f305ddea87f3da713d46
|
|
| MD5 |
4810f6615cb96f50f2ec5dc3bb39242c
|
|
| BLAKE2b-256 |
d04bd6e40403ae0b3437bccc787db568c8c53aad88ad8181b1cc215492b937b2
|
Provenance
The following attestation bundles were made for doctortest-1.0.1.tar.gz:
Publisher:
python-publish.yml on puzzleYOU/doctortest
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
doctortest-1.0.1.tar.gz -
Subject digest:
bba506177d08ca1d8db8b09202b08160fd151afb7020f305ddea87f3da713d46 - Sigstore transparency entry: 749269916
- Sigstore integration time:
-
Permalink:
puzzleYOU/doctortest@da24e0450b873a5a75e3a9606c62d9ba2dbd5c8c -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/puzzleYOU
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@da24e0450b873a5a75e3a9606c62d9ba2dbd5c8c -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38700cfaedd385258c4c27727ec7e7df885f0757de72ff79f12c808ca439f315
|
|
| MD5 |
75eb456a1aa1c75872b8f28ce40b115c
|
|
| BLAKE2b-256 |
00023b8d4f07da6ee09af43167c9472f68321974a50e2a64cc3700373f2fb87b
|
Provenance
The following attestation bundles were made for doctortest-1.0.1-py3-none-any.whl:
Publisher:
python-publish.yml on puzzleYOU/doctortest
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
doctortest-1.0.1-py3-none-any.whl -
Subject digest:
38700cfaedd385258c4c27727ec7e7df885f0757de72ff79f12c808ca439f315 - Sigstore transparency entry: 749269918
- Sigstore integration time:
-
Permalink:
puzzleYOU/doctortest@da24e0450b873a5a75e3a9606c62d9ba2dbd5c8c -
Branch / Tag:
refs/tags/1.0.1 - Owner: https://github.com/puzzleYOU
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@da24e0450b873a5a75e3a9606c62d9ba2dbd5c8c -
Trigger Event:
release
-
Statement type: