Skip to content

doctest cannot be used to test markdown files. #116546

@MusicalNinjaDad

Description

@MusicalNinjaDad

Feature or enhancement

Proposal:

While core python uses reStructuredText, many packages maintain their documentation in markdown.

IDEs such as vscode show docstrings in popups when using a function and render markdown within those popups.

Doctest provides an amazing framework to easily validate the quality of examples given in docstrings and documentation, it is a best-of-breed approach which encourages documentation as code by allowing documentation-testing. (In the opinion of the author - thank you for creating it!)

Unfortunately doctest is currently incompatible with markdown documentation (and docstrings) as it considers the end of a code block, signified by triple-ticks (```) to be part of the expected output. This creates a barrier to developers to implementing best practices as they are not able to test their documentation or docstrings without being tied to a specific form of markup (rst).

Proposal

I have raised a draft pull request implementing one possible, and very simple, approach to providing compatibility with markdown codeblocks.

If you believe that this is a change needing wider discussion, then I will happily open a thread under discuss.python.org. Given the small nature of the change (1 line of library code, 100 lines of test case, 10 lines of documentation), I am awaiting feedback on this issue & draft PR, in case it is considered a simple, non-breaking, adjustment which does not require significant, wider discussion.

Alternatives considered

  1. Creating a dedicated parser.
    • Pros: Could be provided as a module outside the standard library; able to identify start and end of code blocks and use this information in structuring the results.
    • Cons: doctest.testmod() would need to be adjusted to allow for an optional parser keyword argument analog doctest.testfile(); higher barrier to entry for users, need to install a separate package and specifically select a different parser.
  2. Test framework plugins (e.g. pytest-doctest-mkdocstrings).
    • Pros: Can be provided as a module outside the standard library
    • Cons: Requires monkeypatching internals of doctest - prone to future failures; only works when tests are run via a separate test framework
  3. Providing additional markdown parsing within doctest.DocTestParser
    • Pros: Additional structure to doctest results.
    • Cons: Significant additional complexity, goes against the current simplicity and agnostic nature of doctest.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions