Support backtick code blocks in doctest

Currently, doctest requires the use of the >>> prompt format to parse and validate examples. This is effective for interactive-style examples, but not for other docstring formats, such as Google-style or Markdown-like backtick code blocks, which are common in many Python projects.

Proposal

Add an option in doctest to support code blocks enclosed in backticks (```) as an alternative to the >>> prompts. This would allow users to write examples in more modern documentation styles while still leveraging doctest for validation.

This would encourage broader adoption of doctest in projects using Google-style or Markdown-like documentation and might improve flexibility and readability for documentation while maintaining testable examples.

1 Like

Maybe I am missing something and the answer is obvious, but if there are no >>> prompts, there is also no expected output. How would doctest then know what to check for?

5 Likes

I might be missing what is asked for here but it is common to want to have doctest examples with the >>> prompt in .md files that are part of the docs e.g.:

```
>>> 1 + 1
2
```

The problem is that the stdlib doctest just searches for the prompt with a regex that will not find the example shown above unless blank lines are added e.g.:

```

>>> 1 + 1
2

```

Of course if this is part of the documentation then you don’t want the blank lines.

See also this pytest-doctestplus issue and this cpython issue.

You can use assert statements.

1 Like