API Reference¶
Decorator¶
- class doctestcase(globals=None, options=0, **kwargs)[source]¶
Class decorator that turns on evaluation of docstring doctests in subclasses of
unittest.TestCase.- Parameters:
globals (
dict|None, optional) – dictionary of globals passed to the doctest; defaults toNone(no additional globals).options (
int, optional) –doctestoptions, passed todoctest.DocTestRunner; defaults to0(no options).kwargs (
dict, optional) – additional keyword arguments that will be stored under__doctestcase__.kwargsand can be used insetUp(),tearDown(), and custom test methods ofTestCase.
- globals¶
gobalspassed to decorator.- Type:
dict
- options¶
optionspassed to decorator.- Type:
int
- kwargs¶
**kwargspassed to decorator.- Type:
dict
The decorator object, after being applied to the decorated class, stores its copy under attribute
__doctestcase__, includingoptionsand shallow copies of originalglobalsandkwargs.New test method
test_docstring, implementing docstring evaluation, is added to the decorated class. If the decorated class has no docstring or the docstring is blank,test_docstringdoes nothing.The decorated class, as a subclass of
unittest.TestCase, can definesetUp(),tearDown, and its own test methods (exepttest_docstring) that are executed before or after the docstring.If decorated class already has
__doctestcase__attribute (obtained from decoration or inherited from parent classes), it is replaced with a copy;globalsandkwargsare updated with values from the decorator, andoptionsis OR’ed with decorator’soptions. This allows to extend test cases with multiple decoration and inheritance. This also ensures that__doctestcase__attributes of subsequent classes are independent, but values ofglobalsandkwargsdictionaries reference the same objects.The
doctestcaseobject, after being applied toTestCaseclass, can be further reused to decorate otherTestCaseclasses. The same is true for__doctestcase__attribute.When
TestCaseis inherited, the inherited class must be decorated withdoctestcaseagain.Example
from doctest import ELLIPSIS from unittest import TestCase from doctestcase import doctestcase @doctestcase(globals={'X': 'yz'}, options=ELLIPSIS) class SimpleCase(TestCase): """ Title Paragraph. >>> X * 100 'yzyz...' Another paragraph. >>> None >>> True True """
See also
Use cases documentation section contains more examples and use cases.
Formatting¶
- to_markdown(item, title_depth=2, dedent=True, include_title=True)[source]¶
Convert docstring to Markdown.
The first block of non-blank lines up to first blank line represents test case title. It is joined in one line and formatted as section heading.
Every doctest block is formatted as code block.
- Parameters:
item (
object|str|None) – input to be converted. Ifitemisstr, it will be used as input, otherwiseitem.__doc__will be used. If input is blank orNone, empty string is returned.title_depth (
int|None, optional) – heading level for test case title; defaults to2. IfNone, title is not matched and becomes a part of the body text.dedent (
bool, optional) –textwrap.dedentis applied to the docstring by default; this can be turned off by passingdedent=False.include_title (
bool, optional) – whether to include docstring title in the output; defaults toTrue.
- Returns:
Markdown formatted text; may be empty.
- Return type:
str
Example
For
SimpleCasefromdoctestcaseexample,>>> from doctestcase import to_markdown >>> to_markdown(SimpleCase)
## Title Paragraph. ```pycon >>> X * 100 'yzyz...' ``` Another paragraph. ```pycon >>> None >>> True True ```
- to_rest(item, title_char='-', dedent=True, include_title=True)[source]¶
Convert docstring to reStructuredText.
The first block of non-blank lines up to first blank line represents test case title. It is joined in one line and formatted as section heading.
Every doctest block is formatted as code block.
- Parameters:
item (
object|str|None) – input to be converted. Ifitemisstr, it will be used as input, otherwiseitem.__doc__will be used. If input is blank orNone, empty string is returned.title_char (
str|None, optional) – heading underline character for test case title; defaults to'-'. IfNone, title is not matched and becomes a part of the body text.dedent (
bool, optional) –textwrap.dedentis applied to the docstring by default; this can be turned off by passingdedent=False.include_title (
bool) – whether to include docstring title in the output; defaults toTrue.
- Returns:
reST formatted text; may be empty.
- Return type:
str
Example
For
SimpleCasefromdoctestcaseexample,>>> from doctestcase import to_rest >>> to_rest(SimpleCase)
Title ----- Paragraph. >>> X * 100 'yzyz...' Another paragraph. >>> None >>> True True
Docstring components¶
- get_title(item)[source]¶
Get title component of the docstring.
Title is the first block of non-blank lines up to the first blank line. Title lines, if multiple, are joined.
- Parameters:
item (
object|str|None) – input to be converted. Ifitemisstr, it will be used as input, otherwiseitem.__doc__will be used. If input is blank orNone, empty string is returned.- Returns:
may be empty string.
- Return type:
str
- get_body(item, remove_title=True, dedent=True)[source]¶
Get body component of the docstring.
Body is the rest of the text after removing title, and, if not empty, always ends with newline.
- Parameters:
item (
object|str|None) – input to be converted. Ifitemisstr, it will be used as input, otherwiseitem.__doc__will be used. If input is blank orNone, empty string is returned.remove_title (
bool, optional) – whether to remove title; defaults toTrue.dedent (
bool, optional) – whether to applytextwrap.dedentfirst; defaults toTrue.
- Returns:
may be empty string.
- Return type: